查找子节点数最少的数组键



我有一个像这样的数组:

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);
        }

你可以这样做:

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

相关内容

  • 没有找到相关文章

最新更新