I have managed to get to the stage where I have an array that looks like this. Used (zend_json to decode a json response)
Array
(
[response] => Array
(
[status] => ok
[userTier] => free
[total] => 10
[startIndex] => 1
[pageSize] => 10
[currentPage] => 1
[pages] => 1
[results] => Array
(
[0] => Array
(
[id] => lifeandstyle/series/cycling
[type] => series
[webTitle] => Cycling
[webUrl] => http://www.guardian.co.uk/lifeandstyle/series/cycling
[apiUrl] => http://content.guardianapis.com/lifeandstyle/series/cycling
[sectionId] => lifeandstyle
[sectionName] => Life and style
)
[1] => Array
(
[id] => sport/cycling
[type] => keyword
[webTitle] => Cycling
[webUrl] => http://www.guardian.co.uk/sport/cycling
[apiUrl] => http://content.guardianapis.com/sport/cycling
[sectionId] => sport
[sectionName] => Sport
)
How would I go about parsing only the elements that are [webTitle] and [webUrl]
Thanks!
David
73.7k19 gold badges134 silver badges177 bronze badges
asked Dec 14, 2011 at 3:47
1 Answer 1
You can't specifically parse only those parts, but you can iterate over the results and access them.
foreach ($val['response']['results'] as $result) {
$title = $result['webTitle'];
$url = $result['webUrl'];
// ...
}
answered Dec 14, 2011 at 3:50
4 Comments
DIM3NSION
Thanks :) would my syntax be - require_once 'Zend/Json.php'; //load Json library $val = Zend_Json::decode($response_api); //decode he result into something thats parseable foreach ($array['response']['results'] as $val) { $title = $result['webTitle']; $url = $result['webUrl']; // ... }
loganfsmyth
You didn't give the name of your variable in your post, so I used
$array
, but it should be $val
DIM3NSION
perfect, one more question wouldnt i make it so the webUrl was a clickable link when echoed out
loganfsmyth
Any logic you have for displaying this is up to you. Just output the values into HTML however you would do it normally.
lang-php