查找具有最少子项数的数组键

时间:2016-10-30 20:40:07

标签: php arrays multidimensional-array

我有一个如下所示的数组:

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
        )

)

我想返回具有最少子节点的数组的键。在这种情况下,子数组中9个值的任何键。这将是while循环的一部分,并且数组将在循环中丢失一个值。所以第二次,大多数会有8个值,然后是7个等等......

更新

我做到了:

        $arrPotentialPicksTemp = array();

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

但我被卡住了。

1 个答案:

答案 0 :(得分:3)

你可以这样做:

<?php

$array = []; // Your array

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

echo $minNumber;
在我们的示例中,

array_map将回调应用于数组的每个元素($array)。所以,这个匿名函数:

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

返回父数组的每个元素的元素总数。 因此

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

返回一个数组,其中每个元素都是子数组元素的计数。

然后我们应用min函数,它返回数组的最小值。简单。

修改

由于您需要元素数量最少的元素的

$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];