在多维数组中查找最小值并返回键

时间:2022-06-06 21:19:39

I have read several similar questions on here, such as this, Finding the minimum value's key in an associative array but I think my problem may be unique in that my source array is not strings as keys.

我在这里已经阅读了几个类似的问题,例如,在关联数组中查找最小值的键但我认为我的问题可能是唯一的,因为我的源数组不是字符串作为键。

My source array looks like this,

我的源数组看起来像这样,

$dealers = array(
    array(
        [id] => 1526,
        [count] => 2
    ),
    array(
        [id] => 1518,
        [count] => 5
    ),
    array(
        [id] => 1511,
        [count] => 9
    ),
    array(
        [id] => 1410,
        [count] => 3
    )
);

I need to get the id of the smallest count value.

我需要得到最小计数值的id。

I have tried the following,

我试过以下,

$low_dealer = array_keys($dealers, min($dealers));

$ low_dealer = array_keys($ dealer,min($ dealer));

But it appears to be returning the index of the lowest id and not count.

但它似乎返回了最低id的索引,而不是计数。

My next attempt was combining another function I found to find the min of the specific column,

我的下一次尝试是结合我找到的另一个函数来找到特定列的min,

$low_dealer = array_keys($dealers, min( array_column( $dealers, 'count' ) ));

$ low_dealer = array_keys($ dealer,min(array_column($ dealers,'count')));

But that returned nothing.

但这没有任何回报。

EDIT: Also must be able to handle multiple mins, if two or more have the same count number, need to get an array of them back so I can rand() it.

编辑:也必须能够处理多个分钟,如果两个或多个具有相同的计数,需要得到它们的数组,所以我可以rand()它。

Would appreciate any tips here, thank you!

非常感谢这里的任何提示,谢谢!

2 个解决方案

#1


1  

$dealersMin = min(array_column($dealers, 'count'));

$dealersWithMinCount = array_filter($dealers, function ($dealer) {
    global $dealersMin;
    return ($dealer['count'] == $dealersMin);
});

var_dump($dealersWithMinCount[array_rand($dealersWithMinCount)]['id']);

eval.in demo

Explanation

  1. First we find the lowest value of 'count' in the array and save that to $dealersMin.
  2. 首先,我们在数组中找到'count'的最低值,并将其保存到$ dealerMin。

  3. Then we need to get all rows in the $dealers array that have a count of $dealersMin and save that in $dealersWithMinCount.
  4. 然后我们需要获取$ dealers数组中所有具有$ dealerMin计数的行,并将其保存在$ dealersWithMinCount中。

  5. Then just pick a random element of $dealersWithMinCount with array_rand()
  6. 然后用array_rand()选择$ dealersWithMinCount的随机元素

#2


1  

// indexed array: 2, 5, 9, 3
$counts = array_column($dealers, 'count');

// find index of min value
$index = array_search(min($counts), $counts, true);

// $dealers[$index]['id'];

#1


1  

$dealersMin = min(array_column($dealers, 'count'));

$dealersWithMinCount = array_filter($dealers, function ($dealer) {
    global $dealersMin;
    return ($dealer['count'] == $dealersMin);
});

var_dump($dealersWithMinCount[array_rand($dealersWithMinCount)]['id']);

eval.in demo

Explanation

  1. First we find the lowest value of 'count' in the array and save that to $dealersMin.
  2. 首先,我们在数组中找到'count'的最低值,并将其保存到$ dealerMin。

  3. Then we need to get all rows in the $dealers array that have a count of $dealersMin and save that in $dealersWithMinCount.
  4. 然后我们需要获取$ dealers数组中所有具有$ dealerMin计数的行,并将其保存在$ dealersWithMinCount中。

  5. Then just pick a random element of $dealersWithMinCount with array_rand()
  6. 然后用array_rand()选择$ dealersWithMinCount的随机元素

#2


1  

// indexed array: 2, 5, 9, 3
$counts = array_column($dealers, 'count');

// find index of min value
$index = array_search(min($counts), $counts, true);

// $dealers[$index]['id'];