0

Basically, I'm receiving an array() from the Yahoo Messenger API in PHP and am in the process of developing an notification system, It returns an array with both the IM received from an chat and my contacts.

Array (
[0] => Array 
    (
        [message] => Array
            (
                [status] => 1
                [sequence] => 0
                [sender] => SenderCurtis
                [receiver] => receiverCurtis
                [msg] => #1
                [timeStamp] => 1374187598
                [hash] => y2qlDf8uTq8tXzgzrsSMyjQB+W2uDg==
                [msgContext] => y2qlDf8uTq8tXzgzrsSMyjQB+W2uDg==
            )

    )

[1] => Array
    (
        [buddyInfo] => Array
            (
                [sequence] => 1
                [contact] => Array
                    (
                        [0] => Array
                            (
                                [sender] => SenderCurtis
                                [presenceState] => 0
                                [avatarUser] => 0
                                [avatarPreference] => 0
                                [clientCapabilities] => 8915971
                                [clientUserGUID] => MI7STHUYOAMCGE5TNTY7CJPFWM
                            )

                    )

            )

    )

[2] => Array
    (
        [message] => Array
            (
                [status] => 1
                [sequence] => 2
                [sender] => SenderCurtis
                [receiver] => receiverCurtis
                [msg] => #2
                [timeStamp] => 1374187601
                [hash] => 3+2s9sIvjPRdvneQsMgVNCKBTFgKwQ==
                [msgContext] => 3+2s9sIvjPRdvneQsMgVNCKBTFgKwQ==
            )

    )

[3] => Array
    (
        [buddyInfo] => Array
            (
                [sequence] => 3
                [contact] => Array
                    (
                        [0] => Array
                            (
                                [sender] => [email protected]
                                [presenceState] => 0
                                [avatarUser] => 0
                                [avatarPreference] => 0
                                [clientCapabilities] => 8915971
                                [clientUserGUID] => UQU3WV7ZOZ2OTGLJQUE2QJU4ZU
                            )

                    )

            )

    )

)

How can I grab just the message array() and iterate through it? such as "Message 1", "Message2" etc...

5 Answers 5

1

If you're trying to filter the array values for the key 'message', you could do something like this in PHP:

$messages = array();

foreach ($response as $key => $data) {
    if (array_key_exists('message', $data)) {
        $msgArray = $data['message'];
        $messages[] = $msgArray;
    }
}

In the above sample, I'm storing the messages in their own array. But you could process the data right inside the for-loop too, if you want that.

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

Comments

1

I think that array_map() is the function you are looking for here. The array_map function allows you to execute a callback on each element of an existing array and assemble a new array consisting only of the values returned by the callback.

What you would want to do is something like this :

$data = // lets assume this is the data you received
$messages_data = array_map( "extract_message", $data );

function extract_message( $data_item ){
  if ( array_key_exists( 'message', $data_item ) ){
    return $data_item[ 'message' ];
  }
}

Now your $message_data array will contain only the message arrays from the original array.

3 Comments

OK, that's a cleaner solution ;) But you'll probably want to check if the key 'message' exists, in stead of getting undefined index warnings :)
array_filter would suit better I think, because the key message doesn't exists for every entry.
-1 you haven't tested it right? should be array_map('extract_message', $data) + you get empty keys
0
foreach ($var[0] as $key => $value)
{
    do things with each message
}

where $var is substituted for your actual variable name

Comments

0

Just filter your array using array_filter.

  $messages = array_filter($data, function($a){ return isset($a["message"]); });

Then use array_map to get rid of the unwanted intermediate array.

  $messages = array_map(function($a){ return $a["message"]; }, $message);

then you can iterate over it with a foreach or whatever.

Comments

0

Because I like SPL iterators here another solution. Works with PHP >= 5.1.0.

class MessageIterator extends FilterIterator
{
    public function accept()
    {
        return array_key_exists('message', $this->getInnerIterator()->current());
    }

    public function current()
    {
        $current = parent::current();
        return $current['message'];
    }
}

$iterator = new MessageIterator(new ArrayIterator($array));

foreach ($iterator as $message) {
    print_r($message);
}

1 Comment

I like iterators, but the SPL ones are kinda crap. I wish people would start upping to 5.5 so I can finally start using generators.

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.