I am trying to get the values from this multidimentional array.its pattern is as follows by using print_r($link_array);
The array is actually from the 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:
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]))
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;
}
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.