从多维数组返回特定​​值的最高值

时间:2017-03-14 14:57:38

标签: php arrays multidimensional-array gmail-api

我试图从最后一个数组中获取[headers]数组中的值。然后我想将这些值分配给变量以在foreach循环中使用。 [headers]数组有时与其他[headers]数组不同,您可以在下面看到。

第一个[headers]数组在[0] => Array[1] Array下有'发件人'和'日期'。第二个[headers]数组在[11] =>下有'From'和'Date'。数组和[12] =>阵列。

到目前为止,我只是设法使用以下方法获得第一个匹配值:

        $arr = array($array);               
        $arrIt = new RecursiveIteratorIterator(new RecursiveArrayIterator($arr));

        foreach ($arrIt as $sub) {
            $subArray = $arrIt->getSubIterator();
            if ($subArray['name'] === 'From') {
                $outputArray = iterator_to_array($subArray);
            }
        }

使用这个数组:

Array
(
    [id] => 15acc078ff3c13bb
    [historyId] => 30098
    [messages] => Array
        (
            [0] => Array
                (
                    [id] => 15acc078ff3c13bb
                    [threadId] => 15acc078ff3c13bb
                    [labelIds] => Array
                        (
                            [0] => Label_335
                            [1] => IMPORTANT
                            [2] => Label_332
                            [3] => CATEGORY_PERSONAL
                            [4] => INBOX
                        )

                    [snippet] => ok
                    [historyId] => 30084
                    [internalDate] => 1489481730000
                    [payload] => Array
                        (
                            [mimeType] => multipart/alternative
                            [filename] => 
                            [headers] => Array
                                (
                                            [0] => Array
                                                (
                                                    [name] => From
                                                    [value] => google@gmail.com
                                                )
                                            [1] => Array
                                                (
                                                    [name] => Date
                                                    [value] => Tue, 14 Mar 2017 15:55:30 +0700
                                                )
                                        )
        )
            [1] => Array
                (
                    [id] => 15acc09c623d48dd
                    [threadId] => 15acc078ff3c13bb
                    [labelIds] => Array
                        (
                            [0] => SENT
                        )

                    [snippet] => test On Tue, Mar 14, 2017 at 3:55 PM, test user wrote: > ok
                    [historyId] => 30098
                    [internalDate] => 1489481877000
                    [payload] => Array
                        (
                            [partId] => 
                            [mimeType] => text/plain
                            [filename] => 
                            [headers] => Array
                                (
                                            [11] => Array
                                                (
                                                    [name] => From
                                                    [value] => google2@gmail.com
                                                )
                                            [12] => Array
                                                (
                                                    [name] => Date
                                                    [value] => Tue, 14 Mar 2017 15:57:57 +0700
                                                )
                                        )
                                )  
                        )
                )
        )

2 个答案:

答案 0 :(得分:0)

您可以使用array_values重新索引数组。

$maxHistoryId = 0;
$headers = [];
foreach($array['id'] as $v)
{
    if($v['historyId'] >= $max)
    {
        $max = $v['historyId'];
        $headers = $v['payload']['headers'];
    }    
}
$resultArray = array_values($headers);
$resultArray = $resultArray[0];

答案 1 :(得分:0)

您可以执行以下操作,以便使用数组结构迭代所有标头。找到最后一个应该是微不足道的。

foreach($yourArray['messages'] as $messages){
    foreach($messages['payload']['headers'] as $header){
        // header will have those values:
        /*
         * [0] => Array
           (
               [name] => From
               [value] => google@gmail.com
           )

          [1] => Array
          (
              [name] => Date
              [value] => Tue, 14 Mar 2017 15:55:30 +0700
          )

          [0] => Array
          (
              [name] => From
              [value] => google2@gmail.com
          )
          [1] => Array
          (
              [name] => Date
              [value] => Tue, 14 Mar 2017 15:57:57 +0700
          )
         */
    }
}