我有一个看起来像下面的数组。我想在“ diff”索引中找到编号最小的数组。因此,在这种情况下,我想返回数组[7]。我需要的只是数组编号(即7),而不是其他任何信息。
我知道我可以使用array_column轻松找到最低的数字,但是如何返回总的数组索引7?
[1] => Array
(
[qb] => 7
[rb] => 58
[wr] => 91
[te] => 3
[def] => 11
[diff] => 136
)
[2] => Array
(
[qb] => 21
[rb] => 96
[wr] => 102
[te] => 48
[def] => 15
[diff] => 240
)
[3] => Array
(
[qb] => 23
[rb] => 86
[wr] => 216
[te] => 6
[def] => 32
[diff] => 287
)
[4] => Array
(
[qb] => 30
[rb] => 51
[wr] => 200
[te] => 14
[def] => 17
[diff] => 266
)
[5] => Array
(
[qb] => 17
[rb] => 118
[wr] => 273
[te] => 14
[def] => 30
[diff] => 380
)
[6] => Array
(
[qb] => 10
[rb] => 112
[wr] => 142
[te] => 16
[def] => 4
[diff] => 264
)
[7] => Array
(
[qb] => 2
[rb] => 50
[wr] => 135
[te] => 9
[def] => 20
[diff] => 104
)
[8] => Array
(
[qb] => 16
[rb] => 68
[wr] => 141
[te] => 12
[def] => 1
[diff] => 224
)
答案 0 :(得分:5)
如您所说,您可以使用array_column和min()。
然后使用array_search查找最小值所在的位置。
//Since the array starts with key 1 then use array_values to reset the array first.
$array = array_values($array);
$diff = array_column($array, "diff");
$min = min($diff);
$key = array_search($min, $diff);
$result = $array[$key];
工作示例:
https://3v4l.org/ci70K
答案 1 :(得分:3)
以下是示例,我试图实现没有循环但没有成功的解决方案:
$diffs = array_column($array, 'diff'); // get all diffs
$key = min($diffs); // takes the smallest
$result = '';
foreach ($array as $subarray) {
if ($subarray['diff'] == $key) {
$result = $subarray;
}
}
var_dump($result);
$array
是您的数组
返回:
array(6) {
["qb"]=>
int(2)
["rb"]=>
int(50)
["wr"]=>
int(135)
["te"]=>
int(9)
["def"]=>
int(20)
["diff"]=>
int(104)
}
答案 2 :(得分:0)
一种方法可能是使用uasort来维护索引关联,并使用key返回当前数组位置的索引。
uasort($arrays, function ($a, $b) {
return $a["diff"] > $b["diff"];
});
echo key($arrays); // 7
答案 3 :(得分:0)
如果您有多个最小'diff',则可能会遇到问题。 以下内容遍历数组,并在原始键和diff值之间建立映射。
然后使用min来找到最低的差异值。
然后我们搜索与该值对应的键。并通过这些键过滤原始数组。
<?php
$data =
[
'five' => [
'diff'=>5
],
'three' => [
'diff'=>3
],
'seven' => [
'diff'=>3
]
];
foreach($data as $key => $value)
$diffs[$key] = $value['diff'];
$min_diff_keys = array_keys($diffs, min($diffs));
$output = array_intersect_key($data, array_flip($min_diff_keys));
var_export($output);
输出:
array (
'three' =>
array (
'diff' => 3,
),
'seven' =>
array (
'diff' => 3,
),
)
如果您确定diff的唯一唯一最小值,则可以使用diff映射($ diffs)上的array_search而不是array_keys来拉回相关键。并使用该索引来定位所需的数组。
答案 4 :(得分:0)
锅的另一种解决方案( PHP 7.3 + )。我不知道它有多有效。
$diffs = array_column($array, "diff");
// lowest first sort (asort maintains correlation with original indices)
asort($diffs);
$lowest_dif_key = array_key_first($diffs) + 1; // requires PHP 7.3
// as array indices start at zero we have to add one for the correct "array key"
答案 5 :(得分:0)
这个问题值得一个答案,它足够灵活,可以处理“差异”以获得最低的差异,并且计算复杂度为 n 。无需执行多个循环或多个迭代函数调用;和排序技术是无知的。
带有条件块的单个foreach循环简单,高效且易于阅读。
代码:(Demo)
$result = ['lowest' => null, 'indexes' => []];
foreach ($array as $index => $row) {
if ($result['lowest'] === null || $row['diff'] < $result['lowest']) {
$result['lowest'] = $row['diff']; // overwrite
$result['indexes'] = [$index]; // overwrite
} elseif ($row['diff'] == $result['lowest']) {
$result['indexes'][] = $index; // push
}
}
echo "First level index(es) with the lowest diff: " . implode(', ', $result['indexes']) . "\n";
echo "Lowest occurring diff in data set: " . $result['lowest'];
输出:
First level index(es) with the lowest diff: 7
Lowest occurring diff in data set: 104