获取最低键 - 多维数组中的数组值 PHP
Get lowest key - array value in multidimensional array PHP
所以,我得到的数组看起来像这样:
[65]=>
array(2) {
[0]=>
array(2) {
["p"]=>
float(234)
["sp"]=>
float(234)
}
[1]=>
array(2) {
["p"]=>
float(53)
["sp"]=>
float(5)
}
[2]...
[3]...
}
想法是遍历键 65 数组的 0 - N 个值中的每一个,并且只保留具有最小“p”的一个,其他应该被删除/过滤掉。
这应该在 PHP 中完成。
有人知道吗?
我试过这样的事情:
$array = array_filter($array, function ($value, $key) use ($a) {
return $a['p'] <= $value['p'];
}, ARRAY_FILTER_USE_BOTH);
其中 $value 是 65 keyed-array 中元素的 1,$a 是正在动态添加的当前对。因此,每当添加它时,我都会检查现有元素,如果它最低,它应该保留,其他的会立即被过滤掉,但如果它更高,它应该自动被过滤掉。
谢谢!
使用 array_column()
对键 65 内记录的 'p' 值执行 array_multisort()
。
<?php
$col = 'p'; // set the column you want to order on
$column = array_column($arr[65], $col);
array_multisort($column, SORT_ASC, $arr[65]);
$arr[65] = $arr[65][0]; // only keep the record with lowest 'p' value
您可以使用 array_reduce()
获得最低的“p”值:
$arr = [
65 => [
["p" => 234, "sp" => 234],
["p" => 53, "sp" => 5],
["p" => 530, "sp" => 5],
]
];
function getLowestKey($carry, $item) {
if ($item['p'] < $carry || !$carry) {
$carry = $item['p'];
}
return $carry;
}
$lowestKey = array_reduce($arr[65], 'getLowestKey');
var_dump($lowestKey); // int(53)
编辑:
我刚刚注意到您的问题还有第二部分,对此深感抱歉。一旦找到“最低 p”,您就可以使用该知识过滤数组:
$lowestPs = array_filter($arr[65], function($item) use ($lowestKey) {
return $item['p'] == $lowestKey;
});
var_dump($lowestPs);
/*
array(2) {
[1]=>
array(2) {
["p"]=>
int(53)
["sp"]=>
int(5)
}
[2]=>
array(2) {
["p"]=>
int(53)
["sp"]=>
int(5)
}
}
*/
即使多个条目具有相同的最低“p”值(如上例中的 53),此解决方案也有效,所有这些都将保留。
如果有 1 个以上的嵌套级别,您还可以使用递归方法检查 p 的值,使数组保持最低值。
$arrays = [
65 => [
["p" => 234, "sp" => 234],
[
["p" => 53,"sp" => 5],
[
["p" => 54,"sp" => 1],
["p" => 53,"sp" => 7],
]
], [
"p" => 255,
"sp" => 235
],
]
];
function loop($array, &$coll = [], &$min = null)
{
foreach ($array as $key => $value) {
if (is_array($value)) {
loop($value, $coll, $min);
} elseif ($key === "p") {
if ($min === null) $min = $value;
if ($min > $value) {
$coll = [$array];
$min = $value;
continue;
}
if($value === $min) $coll[] = $array;
}
}
return $coll;
}
$arrays[65] = loop($arrays[65]);
var_dump($arrays);
输出
array(1) {
[65]=>
array(2) {
[0]=>
array(2) {
["p"]=>
int(53)
["sp"]=>
int(5)
}
[1]=>
array(2) {
["p"]=>
int(53)
["sp"]=>
int(7)
}
}
}
再看一个php demo。
所以,我得到的数组看起来像这样:
[65]=>
array(2) {
[0]=>
array(2) {
["p"]=>
float(234)
["sp"]=>
float(234)
}
[1]=>
array(2) {
["p"]=>
float(53)
["sp"]=>
float(5)
}
[2]...
[3]...
}
想法是遍历键 65 数组的 0 - N 个值中的每一个,并且只保留具有最小“p”的一个,其他应该被删除/过滤掉。
这应该在 PHP 中完成。 有人知道吗?
我试过这样的事情:
$array = array_filter($array, function ($value, $key) use ($a) {
return $a['p'] <= $value['p'];
}, ARRAY_FILTER_USE_BOTH);
其中 $value 是 65 keyed-array 中元素的 1,$a 是正在动态添加的当前对。因此,每当添加它时,我都会检查现有元素,如果它最低,它应该保留,其他的会立即被过滤掉,但如果它更高,它应该自动被过滤掉。
谢谢!
使用 array_column()
对键 65 内记录的 'p' 值执行 array_multisort()
。
<?php
$col = 'p'; // set the column you want to order on
$column = array_column($arr[65], $col);
array_multisort($column, SORT_ASC, $arr[65]);
$arr[65] = $arr[65][0]; // only keep the record with lowest 'p' value
您可以使用 array_reduce()
获得最低的“p”值:
$arr = [
65 => [
["p" => 234, "sp" => 234],
["p" => 53, "sp" => 5],
["p" => 530, "sp" => 5],
]
];
function getLowestKey($carry, $item) {
if ($item['p'] < $carry || !$carry) {
$carry = $item['p'];
}
return $carry;
}
$lowestKey = array_reduce($arr[65], 'getLowestKey');
var_dump($lowestKey); // int(53)
编辑:
我刚刚注意到您的问题还有第二部分,对此深感抱歉。一旦找到“最低 p”,您就可以使用该知识过滤数组:
$lowestPs = array_filter($arr[65], function($item) use ($lowestKey) {
return $item['p'] == $lowestKey;
});
var_dump($lowestPs);
/*
array(2) {
[1]=>
array(2) {
["p"]=>
int(53)
["sp"]=>
int(5)
}
[2]=>
array(2) {
["p"]=>
int(53)
["sp"]=>
int(5)
}
}
*/
即使多个条目具有相同的最低“p”值(如上例中的 53),此解决方案也有效,所有这些都将保留。
如果有 1 个以上的嵌套级别,您还可以使用递归方法检查 p 的值,使数组保持最低值。
$arrays = [
65 => [
["p" => 234, "sp" => 234],
[
["p" => 53,"sp" => 5],
[
["p" => 54,"sp" => 1],
["p" => 53,"sp" => 7],
]
], [
"p" => 255,
"sp" => 235
],
]
];
function loop($array, &$coll = [], &$min = null)
{
foreach ($array as $key => $value) {
if (is_array($value)) {
loop($value, $coll, $min);
} elseif ($key === "p") {
if ($min === null) $min = $value;
if ($min > $value) {
$coll = [$array];
$min = $value;
continue;
}
if($value === $min) $coll[] = $array;
}
}
return $coll;
}
$arrays[65] = loop($arrays[65]);
var_dump($arrays);
输出
array(1) {
[65]=>
array(2) {
[0]=>
array(2) {
["p"]=>
int(53)
["sp"]=>
int(5)
}
[1]=>
array(2) {
["p"]=>
int(53)
["sp"]=>
int(7)
}
}
}
再看一个php demo。