dtll2016 2017-08-19 16:18
浏览 52
已采纳

如何比较二维数组与单维数组

For clarity purpose Lets assume i have an array like this

Array
(
    [0] => 1234567
    [1] => 1
    [2] => 189818006457
    [3] => 3
    [4] => 10069992244808
    [5] => 2
    [6] => 100699922448
    [7] => 5
    [8] => 123454b
    [9] => 6
    [10] => 12345674b234
    [11] => 2
    [12] => 10
    [13] => 3
    [14] => 100699922
    [15] => 1
    [16] => 1006999
    [17] => 30
    [18] => 9
    [19] => 36
    [20] => 34
    [21] => 45
    [22] => 567
    [23] => 3
    [24] => 44
    [25] => 3
    [26] => 6788
    [27] => 15
    [28] => 189818006457371
    [29] => 17
    [30] => 123RT454b
    [31] => 5
)

And i want to compare it with the array below. So that when the values of the sub-array1 below matches the values of the array above, the system will print the value preceding the matched values in the single dimensional array above in a group. It will also check if the values in sub-array2 below matches the values of the array above, the system will print the value preceding the matched values in the single dimensional array above in another group-2 and so on.

Array
(
    [0] => Array
        (
            [0] => 1
            [1] => 3
            [2] => 2
            [3] => 5
            [4] => 6
            [5] => 2
            [6] => 3
            [7] => 1
            [8] => 3
        )
[2] => Array
    (
        [0] => 30
        [1] => 36
        [2] => 45
    )

[1] => Array
    (
        [0] => 15
        [1] => 17
    )

)

Sorry am new to php but i have to do the project. Thanks

  • 写回答

1条回答 默认 最新

  • douhua1760 2017-08-22 13:06
    关注

    I would suggest to first rearrange a bit the data that you have:

    • Create a map for the array of subarrays, which for a given value will return the number of the group.

    • Turn the 1D input array into an array of pairs

    Then you can iterate the array of pairs and perform a lookup in the map.

    Here is the code:

    $input = [ 
        '1234567', 1, '189818006457', 3, '10069992244808', 2,
        '100699922448', 5, '123454b', 6, '12345674b234', 2,
        '10', 3, '100699922', 1, '1006999', 30, '9', 36,
        '34', 45, '567', 3, '44', 3, '6788', 15,
        '189818006457371', 17, '123RT454b', 5
    ];
    
    $groups = [
        [1, 3, 2, 5, 6, 2, 3, 1, 3], 
        [30, 36, 45], 
        [15, 17]
    ];
    
    // First create an associative array that maps a given value to a group number
    $map = [];
    foreach ($groups as $group => $subarray) {
        $map = $map + array_combine($subarray, array_fill(0, count($subarray), $group));
    }
    // Now iterate pairs of the input data
    foreach (array_chunk($input, 2) as list($v, $k)) {
        $result[$map[$k]][] = $v ;
    }
    

    The $result variable will have three subarrays, one per group:

    [
        [
            '1234567', '189818006457', '10069992244808',
            '100699922448', '123454b', '12345674b234',
            '10', '100699922', '567', '44', '123RT454b'
        ], [
            '1006999', '9', '34'
        ], [
            '6788', '189818006457371'
        ]
    ]
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据