开发者

Trouble displaying variable in foreach loop using multidimentional array

开发者 https://www.devze.com 2023-04-06 20:51 出处:网络
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'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 />';
}


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号