2017-04-01 44 views
0

我有這個非常困難的數組迭代。我只想要name指數的價值。我試了很多,但不知道如何找到這個。如何在PHP中迭代這個多維數組?

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)) 

下面是我的一些代碼的做法:

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


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


} 

任何幫助,將不勝感激。由於

+0

這不是一個複雜的陣列。這完全是爲自己(和我們?)組織數據的問題。顯然,這個名字是'from'值中數組的一部分。你必須考慮到這一點。 –

+0

@KIKOSoftware。這是動態數組。我無法手動改變它。我必須動態迭代它 –

+0

您對我所說的有反應嗎?在那種情況下,我不理解它。你的意思是你根本不知道數組的結構? –

回答

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

謝謝各不相同 –

1

數據:

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) 
    ) 

這也將消除重複的名稱(每個名稱將只顯示一次)。

$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; 
}