[英]How to analyse using foreach loop on this type of multidimensional array?
I am trying to get the values from this multidimentional array.its pattern is as follows by using print_r($link_array);
我正在尝试从这个多维数组中获取值。通过使用print_r($link_array);
其模式如下
The array is actually from the google rss api. 该数组实际上来自google rss api。
Array ( [0] => Array ( [title] => World Cup draw -- group-by-group analysis [link] => http://www.dawn.com/news/1061096/world-cup-draw-group-by-group-analysis [author] => [publishedDate] => Sat, 07 Dec 2013 02:31:51 -0800 [contentSnippet] => GROUP A: Brazil, Croatia, Mexico, Cameroon - Having hit form in spectacular style earlier this year by winning the ... [content] =>asdfasdf
[categories] => Array ( [0] => Sport/Football ) ) [1] => Array ( [mediaGroups] => Array ( [0] => Array ( [contents] => Array ( [0] => Array ( [url] => http://i.dawn.com/primary/2013/12/52a2f7a584e2a.jpg [type] => image/jpeg [medium] => image [title] => Brazil's head coach Luiz Felipe Scolari. -Photo by AP [thumbnails] => Array ( [0] => Array ( [url] => http://i.dawn.com/primary/2013/12/52a2f7a584e2a.jpg ) ) ) ) ) )
I have tried following three ways 我尝试了以下三种方法
echo $link_array[0];
echo $link_array[title];
foreach($link_array as $key=>$value){
echo $key;
}
Pleas help. 请帮助。
Your guess is mostly right, however you're printing its keys, just try acessing value['title']
and you'll get the title: 您的猜测基本上是正确的,但是您正在打印其键,只需尝试访问value['title']
即可获得标题:
foreach($link_array as $key=>$value){
echo $value['title'];
}
You need to first assign the array to a variable. 您需要首先将数组分配给变量。
$link_array = Array ( [0] => Array ( [title] => World Cup draw -- group-by-group analysis [link] => http://www.dawn.com/news/1061096/world-cup-draw-group-by-group-analysis [author] => [publishedDate] => Sat, 07 Dec 2013 02:31:51 -0800 [contentSnippet] => GROUP A: Brazil, Croatia, Mexico, Cameroon - Having hit form in spectacular style earlier this year by winning the ... [content] =>asdfasdf
[categories] => Array ( [0] => Sport/Football ) ) [1] => Array ( [mediaGroups] => Array ( [0] => Array ( [contents] => Array ( [0] => Array ( [url] => http://i.dawn.com/primary/2013/12/52a2f7a584e2a.jpg [type] => image/jpeg [medium] => image [title] => Brazil's head coach Luiz Felipe Scolari. -Photo by AP [thumbnails] => Array ( [0] => Array ( [url] => http://i.dawn.com/primary/2013/12/52a2f7a584e2a.jpg ) ) ) ) ) )
then you can do excatly as you have done 那么你就可以像你一样出色地做
foreach ($link_array as $key=>$value):
echo $key . " - " . $value;
endforeach;
but this will not display your internal arrays within the array. 但这不会在阵列中显示您的内部阵列。 For that you would need to do a for each inside a foreach if is_array($link_array[$key]))
为此,如果is_array($link_array[$key]))
则需要在foreach中为每个对象做一个
foreach ($link_array as $key=>$value):
$new = $link_array[$key];
if (is_array($new)):
foreach ($new as $key1=>$value1):
echo $key1 . " - " . $value1;
endforeach;
else:
echo $key . " - " . $value;
endif;
endforeach;
You can go as many levels deep as you want and change the echo. 您可以根据需要深入任意多个级别并更改回声。
You could also call a function within a function: 您还可以在一个函数内调用一个函数:
// call it
loopfor($link_array);
function loopfor ($DATA) {
foreach ($DATA as $key=>$value):
if (is_array($DATA[$key])):
loopfor($DATA[$key]);
else:
echo $key . " - " . $value;
endif;
endforeach;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.