dousuiben8395 2016-10-30 20: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 20: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];
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算