dousuiben8395 2016-10-30 12:40
浏览 33
已采纳

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

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.

展开全部

  • 写回答

1条回答 默认 最新

  • drhqkz3455 2016-10-30 12:53
    关注

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

    展开全部

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部