0

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!

1 Answer 1

4

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'];

  // ...
}
Sign up to request clarification or add additional context in comments.

4 Comments

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']; // ... }
You didn't give the name of your variable in your post, so I used $array, but it should be $val
perfect, one more question wouldnt i make it so the webUrl was a clickable link when echoed out
Any logic you have for displaying this is up to you. Just output the values into HTML however you would do it normally.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.