[英]return values from multidimensional array by the highest value of a specific value
I am trying to get the values in the [headers] array from the last array number. 我试图从最后一个数组编号中获取[headers]数组中的值。 I then want to assign these values to variables to use in a foreach loop.
然后我想将这些值分配给变量以在foreach循环中使用。 The [headers] array numbers are sometimes different to other [headers] arrays, you can see this below.
[headers]数组有时与其他[headers]数组不同,您可以在下面看到。
The first [headers]
array has 'From' and 'Date' under [0] => Array
and [1] Array
. 第一个
[headers]
数组在[0] => Array
和[1] Array
下有'From'和'Date'。 The second [headers] array has 'From' and 'Date' under [11] => Array and [12] => Array. 第二个[headers]数组在[11] => Array和[12] => Array下有'From'和'Date'。
So far I have only managed to get a first matched value using: 到目前为止,我只使用以下方法获得第一个匹配值:
$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);
}
}
Using this array: 使用这个数组:
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
)
)
)
)
)
)
you can use array_values to reindex the array. 您可以使用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];
You could do the following in order to iterate all headers with the structure of your array. 您可以执行以下操作,以便使用数组结构迭代所有标头。 Finding the last one should be trivial.
找到最后一个应该是微不足道的。
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
)
*/
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.