0

基本上,我array()PHP 中的 Yahoo Messenger API收到一个,并且正在开发一个通知系统,它返回一个数组,其中包含从聊天中收到的IM和我的联系人。

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] => myContactUser1@yahoo.com
                                [presenceState] => 0
                                [avatarUser] => 0
                                [avatarPreference] => 0
                                [clientCapabilities] => 8915971
                                [clientUserGUID] => UQU3WV7ZOZ2OTGLJQUE2QJU4ZU
                            )

                    )

            )

    )

)

我怎样才能抓住消息 array()并遍历它?例如“消息 1”、“消息 2”等...

4

5 回答 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.

于 2013-07-18T23:08:42.223 回答
1

我认为这array_map()就是您在这里寻找的功能。该array_map函数允许您对现有数组的每个元素执行回调,并组装一个仅由回调返回的值组成的新数组。

你想要做的是这样的:

$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' ];
  }
}

现在您的$message_data数组将只包含原始数组中的消息数组。

于 2013-07-18T23:07:44.170 回答
0
foreach ($var[0] as $key => $value)
{
    do things with each message
}

where$var代替您的实际变量名称

于 2013-07-18T23:07:26.800 回答
0

因为我喜欢这里的 SPL 迭代器,这是另一种解决方案。适用于 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);
}
于 2013-07-18T23:33:37.240 回答
0

只需使用array_filter过滤您的数组。

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

然后使用array_map摆脱不需要的中间数组。

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

然后您可以使用 foreach 或其他方法对其进行迭代。

于 2013-07-18T23:13:53.740 回答