0

I have the following array

Array
(
[responseData] => Array
      (
         [results] => Array
            (
                [0] => Array
                    (
                        [GsearchResultClass] => GwebSearch
                        [unescapedUrl] => http://en.wikipedia.org/wiki/JH_(hash_function)
                        [url] => http://en.wikipedia.org/wiki/JH_(hash_function)
                        [visibleUrl] => en.wikipedia.org
                        [cacheUrl] => http://www.google.com/search?q=cache:jxOefvJSQXUJ:en.wikipedia.org
                        [title] => JH (hash function) - Wikipedia, the free encyclopedia
                        [titleNoFormatting] => JH (hash function) - Wikipedia, the free encyclopedia
                        [content] => JH is a cryptographic hash function submitted to the NIST hash function   competition by Hongjun Wu. Though chosen as one of the five finalists of the   competition ...
                    )

                [1] => Array
                    (
                        [GsearchResultClass] => GwebSearch
                        [unescapedUrl] => http://www.jhaudio.com/
                        [url] => http://www.jhaudio.com/
                        [visibleUrl] => www.jhaudio.com
                        [cacheUrl] => http://www.google.com/search?q=cache:rO6NylpvTx8J:www.jhaudio.com
                        [title] => JH Audio: Custom In-Ear Monitors | In Ear Monitor
                        [titleNoFormatting] => JH Audio: Custom In-Ear Monitors | In Ear Monitor
                        [content] => Custom In-Ear Monitors by JHAudio - manufacturers of premium custom in ear   monitors. JH Audio's products are a direct result of 25 years of live audio mixing ...
                    )

                [2] => Array
                    (
                        [GsearchResultClass] => GwebSearch
                        [unescapedUrl] => http://www.jhaudio.com/collection/jha-pro-music-products
                        [url] => http://www.jhaudio.com/collection/jha-pro-music-products
                        [visibleUrl] => www.jhaudio.com
                        [cacheUrl] => http://www.google.com/search?q=cache:YY9q-E00yKkJ:www.jhaudio.com
                        [title] => JHA Pro Music Products | Custom In-Ear Monitors by JH Audio
                        [titleNoFormatting] => JHA Pro Music Products | Custom In-Ear Monitors by JH Audio
                        [content] => JHA Pro Music Products by JHAudio - manufacturers of premium custom in ear   monitors. JH Audio's products are a direct result of 25 years of live audio mixing ...
                    )

                [3] => Array
                    (
                        [GsearchResultClass] => GwebSearch
                        [unescapedUrl] => http://www.youtube.com/watch?v=JsQN3yZjRCI
                        [url] => http://www.youtube.com/watch%3Fv%3DJsQN3yZjRCI
                        [visibleUrl] => www.youtube.com
                        [cacheUrl] => http://www.google.com/search?q=cache:Dk4oETmQLNEJ:www.youtube.com
                        [title] => monta equina zagalo de j.h x gitana de la fortuna - YouTube
                        [titleNoFormatting] => monta equina zagalo de j.h x gitana de la fortuna - YouTube
                        [content] => Mar 5, 2010 ... caballos de exposicion en la modalidad de trote y galope, fecha de monta 1 de   febrero de 2010....
                    )

            )

I'm trying to parse it in php using a foreach loop so the url, title and content will be shown. However I can't seem to parse the code properly. The code below gives me an error that 'responsedata' isin't recognisied.

foreach($json as value)

echo $value [responsedata];

When I leave it at echo $value, it gives me the number 200, which is the value of responsestatus. When I try

foreach( $json =>url => title => content as $value)

it won't recognise the '=' sign.

Any ideas?? I'm not overly familiar with JSON or php if you havn't gotten that from the post:)

TIA

2 Answers 2

1

FTR, what you posted is a native array, not a "json array", try this:

$rawArray = get_results_somehow();

// Flatten the array to make it easier to work with
$results = $rawArray['responseData']['results'];

foreach ($results as $result) {

    // Should now see the expected values
    var_dump($result);
}
Sign up to request clarification or add additional context in comments.

Comments

1

That data is already in array form. You need to iterate on $array['responseData']['results'] like this.

$arr = $array['responseData']['results'];

foreach($arr as $k){
   echo $k['url'];
   echo $k['title'];
   echo $k['content'];
}

Comments

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.