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

报告相同问题?

悬赏问题

  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制