views:

52

answers:

1

I have the following array that I get as an output from facebook:

http://www.paste.to/v/1jntlnml

With the following code:

 $stream = $facebook->api_client->stream_get('',128342905144,'0','0',30,'','','','');



foreach($stream as $wallpost) {
    echo '<pre>';
   print_r($wallpost);
    echo '</pre>';
}

So I get the data that I need, but I want to call the individual variables within this array. For example, echo out the [message] for each post.

Since it only loops once, I cant echo $wallpost['message'] or anything similar.

any idea?

A: 

If the output of echo $wallpost is Array Array, I assume $wallpost contains the arrays from the pastebin, e.g.

$stream = Array(
  // $stream[0]
  Array(
    // $stream[0][0]
    Array('message' => 'msg1'),
    // $stream[0][1]
    Array('message' => 'msg2'),
  ),
  // $stream[0]
  Array(
    /* other stuff */
  )
);

In which case you get the messages by iterating over the first array, containing the messages like this

foreach($stream[0] as $wallposts) {
    echo $wallposts['message'];
}

As an alternative, wrap the returned array into a RecursiveArrayIterator to traverse the entire array in one go:

$iterator = new RecursiveIteratorIterator(
                new RecursiveArrayIterator($stream),
                RecursiveIteratorIterator::SELF_FIRST));

foreach($iterator as $key => $val) {
    if($key === 'message') {
        echo $val;
    }
}

Note: in your case, using the Iterator is rather pointless though because you know the second array does not contain any elements with posts in it and the first array is not nested.

Gordon
Hrm, when I use $stream[0] or $stream[1] I get the following:Notice: Undefined offset: 0
Wes
@Wes that implies $stream has associative keys instead of numeric keys. Can you do a var_export($streams) and put that into the pasty please.
Gordon