2

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.

I have tried the following,

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

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

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

$low_dealer = array_keys($dealers, 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.

Would appreciate any tips here, thank you!

thatryan
  • 1,551
  • 6
  • 21
  • 39

2 Answers2

6
// 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'];
nice ass
  • 16,471
  • 7
  • 50
  • 89
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. Then we need to get all rows in the $dealers array that have a count of $dealersMin and save that in $dealersWithMinCount.
  3. Then just pick a random element of $dealersWithMinCount with array_rand()
Ethan
  • 4,295
  • 4
  • 25
  • 44
  • Added an edit to question, how can it handle if two or more have a min value? – thatryan Jun 20 '17 at 18:24
  • Think Im following that thanks. Throwing me an undefined index though on the var_dump line... – thatryan Jun 20 '17 at 18:43
  • @thatryan Oh, that's strange, is the `$dealers` array different to the one you posted in your question? I tried it in eval.in and it worked fine... Unless the `$dealers` array was empty, in which case you'd need to add checking before using the last line :) – Ethan Jun 20 '17 at 18:45
  • Weird. No same array, just different variable name `$all_dealers` ( i just shortened it to post here ) and it is not empty because the `$dealersMin` get populated with 2, like it should. – thatryan Jun 20 '17 at 19:18
  • No, I went to get lunch ha, back to debugging :) – thatryan Jun 20 '17 at 20:30
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/147213/discussion-between-thatryan-and-david). – thatryan Jun 20 '17 at 22:18
  • Thanks for your help David. I got it to work by adding a global declaration to the initial variable as well. – thatryan Jun 20 '17 at 22:34