0

I have some Json being returned from facebook which I'm then parsing in to an array using json _decode. The data ends up looking like this (this is just the snippet I'm interested in):

( [data] =>
     Array ( [0] => 
         Array ( 
             [id] => 1336269985867_10150465918473072 
             [from] => 
                 Array ( [name] => a name here 
                         [category] => Community 
                         [id] => 1336268295867 ) 
                         [message] => A message here

Now I've been able to iterate over this data and get what I need:

   $jsonDecoded = json_decode($json, true);
   $xmlOutput = '<?xml version="1.0"?><data><items>';
   foreach ($jsonDecoded as $e) {
       foreach ($e as $i) {
           $xmlOutput .= '<item><timestamp>' . $i['created_time'] . '</timestamp><title><![CDATA[ ' . $i['message'] .' ]]></title><link>' . $link . '</link><type>facebook</type></item>';
       }
   }

   $xmlOutput .= '</items></data>';

..up until now where I need to check on the from->id value.

I added this line in the second for each:

 foreach ($e as $i) {
     if($i['from']['id'] == '1336268295867') {

But this just gives me an error:

Fatal error: Cannot use string offset as an array in /Users/Desktop/Webs/php/getFeeds

Any ideas why? I'm sure this is the correct way to get at that value and in actual fact if I echo this out in my loop instead of doing the if statement above I get the value back:

$jsonDecoded = json_decode($json, true);
$xmlOutput = '<?xml version="1.0"?><data><items>';
foreach ($jsonDecoded as $e) {
    foreach ($e as $i) {
       echo $i['from']['id']  

This returns me all of the from->id values in the code returned from facebook and then following this I get the error:

133626829985867133626829985867133626829985867133626829985867195501239202133626829985867133626829985867133626829985867133626829985867133626829985867
Fatal error: Cannot use string offset as an array in /Users/Desktop/Webs/php/getFeeds.php on line 97

(line 97 is the echo line)

1
  • 1
    Mmmm ... i seem to be unable to reproduce your error messages with my test-code. could you add a "print_r" in your loops to print out the contents of $e? Commented Nov 16, 2011 at 11:05

2 Answers 2

2

Your code makes a lot of assumptions about $i['from']['id'] and at least one of them is incorrect for at least one entry.
Let's add some tests:

$jsonDecoded = json_decode($json, true);
$xmlOutput = '<?xml version="1.0"?><data><items>';
foreach ($jsonDecoded as $e) {
    if ( !is_array($e) ) {
        die('type($e)=='.gettype($e).'!=array');
    }
    foreach ($e as $i) {
        if ( !is_array($i) ) {
            die('type($i)=='.gettype($i).'!=array');
        }
        else if ( !array_key_exists('from', $i) ) {
            die('$i has no key "from"');
        }
        else if ( !is_array($i['from']) ) {
            die('type($i["from"])=='.gettype($i['from']).'!=array');
        }
        else if ( !array_key_exists('id', $i['from']) ) {
            var_dump($i);
            die('$i["from"] has no key "id"');
        }

        echo $i['from']['id'];
    }
}

And then you can add a var_dump(...) before the die(...) to take a look at the actual data.

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

1 Comment

Yes this was it. At some point it isn't an array any more. This is confusing as the array returned is well formed. Thanks for the detailed reply
2

It seems to me that (according to the last code snippet) at some point your $i is not an array anymore. Try to do:

$jsonDecoded = json_decode($json, true);
$xmlOutput = '<?xml version="1.0"?><data><items>';
foreach ($jsonDecoded as $e) {
    foreach ($e as $i) {
       if(is_array($i))
           echo $i['from']['id']  

1 Comment

Great this was it. I'm not sure why because the array returned from the json _decode is well formed. Hey ho. It's fixed. Many thanks

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.