jesse94 jesse94 - 1 month ago 8
PHP Question

Find array key who has the least number of children

I have an array that looks like this:

Array
(
[3] => Array
(
[0] => 1363
[1] => 1364
[2] => 5
[3] => 4
[4] => 2
[5] => 1079
[6] => 1366
[7] => 37
[8] => 1398
)

[1363] => Array
(
[0] => 3
[1] => 1364
[2] => 5
[3] => 1
[4] => 4
[5] => 2
[6] => 1079
[7] => 1366
[8] => 1398
)

[1364] => Array
(
[0] => 3
[1] => 1363
[2] => 5
[3] => 1
[4] => 4
[5] => 2
[6] => 1366
[7] => 37
[8] => 1398
)

[5] => Array
(
[0] => 3
[1] => 1363
[2] => 1364
[3] => 1
[4] => 2
[5] => 1079
[6] => 1366
[7] => 37
[8] => 1398
)

[1] => Array
(
[0] => 1363
[1] => 1364
[2] => 5
[3] => 4
[4] => 2
[5] => 1079
[6] => 1366
[7] => 37
[8] => 1398
)

[4] => Array
(
[0] => 3
[1] => 1363
[2] => 1364
[3] => 1
[4] => 2
[5] => 1079
[6] => 1366
[7] => 37
[8] => 1398
)

[2] => Array
(
[0] => 3
[1] => 1363
[2] => 1364
[3] => 5
[4] => 1
[5] => 4
[6] => 1079
[7] => 1366
[8] => 37
)

[1079] => Array
(
[0] => 3
[1] => 1363
[2] => 5
[3] => 1
[4] => 4
[5] => 2
[6] => 1366
[7] => 37
[8] => 1398
)

[1366] => Array
(
[0] => 3
[1] => 1363
[2] => 1364
[3] => 5
[4] => 1
[5] => 4
[6] => 2
[7] => 1079
[8] => 37
[9] => 1398
)

[37] => Array
(
[0] => 3
[1] => 1364
[2] => 5
[3] => 1
[4] => 4
[5] => 2
[6] => 1079
[7] => 1366
[8] => 1398
)

[1398] => Array
(
[0] => 3
[1] => 1363
[2] => 1364
[3] => 5
[4] => 1
[5] => 4
[6] => 1079
[7] => 1366
[8] => 37
)

)


I want to return the key of the array which has the least number of children. In this case, any key that 9 values in the subarray. This will be part of a while loop and the array will lose a value in the loop. So the 2nd time, most will have 8 values, then 7 and so on...

UPDATE:

I made this:

$arrPotentialPicksTemp = array();

foreach ($arrPotentialPicks as $id => $picks) {
$arrPotentialPicksTemp[$id] = count($picks);
}


But I'm stuck.

Answer

You can do it like this:

<?php

$array = []; // Your array

$minNumber =  min(array_map(function($subarray) {
    return count($subarray);
}, $array));

echo $minNumber;

array_map applies a callback to each element of an array ($array) in our case. So, this anonymous function:

function($subarray) {
    return count($subarray);
}

returns total number of elements of each element of the parent array. Thus

array_map(function($subarray) {
    return count($subarray);
}, $array)

returns an array, where each element is a count of elements of subarray.

Then we apply min function, which returns the minumum value of an array. Simple.

EDIT

Since you need the key of the element with least number of elements:

$array = [
    'foo' =>[1, 3, 4, 5],
    'bar' => [1, 3],
    'baz' => [1],
];


// So here is proper, readable version:

$counted = array_map(function($subarray) {
    return count($subarray);
}, $array);

// print_r($counted) outputs
// Array ( [foo] => 4 [bar] => 2 [baz] => 1 )
// So we have keys and element counters as values

// Now let's flip arrays and keys


$flipped = array_flip($counted);

// print_r($counted) outputs 
// Array ( [4] => 'foo' [2] => 'bar' [1]=> 'baz' )
// So we have keys and element counters as values

// Now let's find the minimum key, which will have the final result

$lowestKey = min(array_keys($flipped));

echo $flipped[$lowestKey];