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

Php 按特定值的最高值从多维数组返回值,php,arrays,multidimensional-array,gmail-api,Php,Arrays,Multidimensional Array,Gmail Api,我试图从最后一个数组编号中获取[headers]数组中的值。然后,我想将这些值分配给在foreach循环中使用的变量。[headers]数组编号有时与其他[headers]数组不同,您可以在下面看到这一点 第一个[headers]数组在[0]=>数组和[1]数组下有“From”和“Date”。第二个[headers]数组在[11]=>数组和[12]=>数组下有“From”和“Date” 到目前为止,我仅使用以下方法获得了第一个匹配值: $arr = array($array);

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

第一个
[headers]
数组在
[0]=>数组和
[1]数组下有“From”和“Date”。第二个[headers]数组在[11]=>数组和[12]=>数组下有“From”和“Date”

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

        $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
                                                )
                                        )
                                )  
                        )
                )
        )

您可以使用数组_值来重新索引数组

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

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

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
          )
         */
    }
}

这不会打印任何结果,仅为空。可能是因为我在数组外留了一行,所以我更新了这个问题,您将看到historyId上面的[id]=>15acc7494d76f74e-我想这就是为什么您的代码不工作的原因,还将$maxHistoryId更改为$max