checkenginelight
checkenginelight

Reputation: 1146

Trouble displaying variable in foreach loop using multidimentional array

I'm able to display id and tags in the foreach loop no problem. But I'm having trouble showing title and height. I'm not sure how to call them into the foreach loop.

I can call them in the while loop so I know that they are working.

$persons = array();
$tags = array();

while( ($row = mysqli_fetch_array( $rs, MYSQLI_ASSOC)))
{
    if(!isset( $persons[$row['id']]))
    {
        $persons[$row['id']]['title'] = $row['title'];
        $persons[$row['id']]['height'] = $row['height'];
        $persons[ $row['id'] ] = array( 'id' => $row['id'], 'tag' => $row['tag']);
        $tags[ $row['id'] ] = array();
    }
    $tags[ $row['id'] ][] = $row['tag'];
}

foreach( $persons as $pid => $p)
{
    echo 'id: # ' . $p['id'] ;
    echo 'title: ' . $p['title'];
    echo 'height: ' . $p['height'];
    echo '<a href="#">' . implode( '</a>, <a href="#">', $tags[ $p['id'] ])  . '</a>';
    echo '<br /><br />';
}

Upvotes: 0

Views: 59

Answers (1)

Facundo Farias
Facundo Farias

Reputation: 408

You're overwriting $persons[ $row['id'] ] when you set the tags, so you lost the other data.

Upvotes: 2

Related Questions