简体   繁体   中英

How to iterate this multi dimensional array in php?

I have got this very difficult array to iterate. I want value of only name index. I have tried a lot but not sure how to find this.

Array ( [0] => Array ( [created_time] => DateTime Object 
( [date] => 2017-04-01 12:58:25.000000 [timezone_type] => 1 [timezone] =>
+00:00 ) [from] => Array ( [name] => Ghulam Mohe Ud Din [id] => 
1242172779233884 ) [message] => Hi there[id] => 10154774339307663_10154774534942663 ))

Here is my some practice of code:

foreach ($getPostlikes as $key=> $item) {//suppose array is $getPostlikes 


    foreach($item as $i)
    {
        echo $i['name'];
    }


}

Any help would be appreciated. Thanks

Data:

Array ( 
    [0] => Array ( 
        [created_time] => DateTime Object,
        (
            [date] => 2017-04-01 12:58:25.000000, 
            [timezone_type] => 1, 
            [timezone] => +00:00),
        [from] => Array ( [name] => Ghulam Mohe Ud Din [id] => 1242172779233884 ),
        [message] => Hi there[id] => 10154774339307663_10154774534942663 )
    )

This will also eliminate duplicate names (each name will display only once).

$name_list = array();
foreach($postGetLikes as $postno => $post) {
    if(!isset($name_list[$post['from']['name']])) {
        $name_list[$post['from']['name']] = 0;
    }
    $name_list[$post['from']['name']]++;
}

foreach($name_list as $name => $count) {
    echo $name;
}
foreach ($getPostlikes as $key=> $item) {//suppose array is $getPostlikes 
  echo $item['from']['name'];
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM