Explain how this custom function works PHP

后端 未结 2 765
长情又很酷
长情又很酷 2021-01-18 04:45

Here this function in PHP that allows to merge any N amount of different length arrays in a fashion that output array will be in next order: Array1[0],Array2[0],..,Arr

2条回答
  •  误落风尘
    2021-01-18 05:12

    The arrays $a, $b and $c have 10, 6 and 2 elements respectively.

    $a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
    $b = ['a', 'b', 'c', 'd', 'e', 'f'];
    $c = ['A', 'B'];
    

    When you feed the arrays as arguments for the array_zip_merge() function, look at the for loop. The func_get_args() will set the $args with all the arguments supplied. On start of first for loop run,

    $args = [$a, $b, $c];
    count($args) = 3;
    

    At the foreach loop the array_shift will return the first element of each array resulting the $output to be like

    $output = [1, 'a', 'A'];
    

    And the arrays now look like,

    $a = [2, 3, 4, 5, 6, 7, 8, 9, 10];
    $b = ['b', 'c', 'd', 'e', 'f'];
    $c = ['B'];
    

    At the end of the first for loop the array_filter function will test if any array is empty and remove it from $args. Same thing will happen at the second run, and by the end of the second for loop, the variables would look like

    $a = [3, 4, 5, 6, 7, 8, 9, 10];
    $b = ['c', 'd', 'e', 'f'];
    $c = [];
    $output = $output = [1, 'a', 'A', 2, 'b', 'B'];
    //because $c is empty array_filter() removes it from $args
    $args = [$a, $b];
    

    So, on the third iteration of the for loop count($args) will return 2. When the last element of $b has been removed by array_shift the count($args) will return 1. The iteration will continue until all the arrays are empty

提交回复
热议问题