0

I'm building a JSON array as follows:

 $footerSEOArray[$data['domainname']] = $data[$language];
 echo json_encode($footerSEOArray);

Once passed back I decode it like this:

 $footerLinks = json_decode($result);

I can print the array as follows:

 print_r($footerLinks);

And when printed it looks like this:

    stdClass Object
 (
[www.data1.com] => Australia
[www.data2.com] => Hindu
[www.data3.com] => Laos
[www.data4.com] => Iranian
[www.data5.com] => America
)

Now I need to be able to print the first and second parts of the array but I can't seem to get it to print outside of the print_r();

thx

3
  • It is an object not an array. you should learn how to access object property or array items. Commented Jan 2, 2012 at 12:03
  • 2
    $footerLinks is an object. You probably want to get it as array, for which you have to pass true as second parameter to json_decode. You can then iterate over the array. Commented Jan 2, 2012 at 12:04
  • You should be using echo $footerLinks[0]->{'www.data1.com'}; Commented Jan 2, 2012 at 12:08

4 Answers 4

1

You have to pass to json_decode an additional parameter to obtain an array instead that a generic array.

 $footerLinks = json_decode($result, true);

Reference: The php manual json_decode page

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

Comments

1

You should pass the second parameter to the json_decode function. It's default behaviour is to convert key-value pairs to the stdClass object, but with this parameter you force using associative table instead.

So:

 $footerLinks = json_decode($result, true);

Comments

0

Try:


$footerLinks = (array) $footerLinks;

Comments

0

As all said $footerlinks is an object , if you want to use it like an array you've to decode like this:

 $footerLinks = json_decode($result,true);

Now, $footLinks becomes an associative array.

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.