解释这个自定义函数是如何工作的

时间:2021-08-11 22:29:40

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],..,ArrayN[0],Array1[1],Array2[1],..,ArrayN[1]... :

这里的PHP函数允许以以下顺序合并任意N个不同长度的数组:Array1[0] Array2[0] ArrayN[0] Array1[1] Array2[1] ArrayN[1]…:

    function array_zip_merge() {
      $output = array();
      // The loop incrementer takes each array out of the loop as it gets emptied by array_shift().
      for ($args = func_get_args(); count($args); $args = array_filter($args)) {
        // &$arg allows array_shift() to change the original.
        foreach ($args as &$arg) {
          $output[] = array_shift($arg);
        }
      }
      return $output;
    }

// test

$a = range(1, 10);
$b = range('a', 'f');
$c = range('A', 'B');
echo implode('', array_zip_merge($a, $b, $c)); // prints 1aA2bB3c4d5e6f78910

While I understand what each of built in functions in this example do on their own, I just cant wrap my mind how it all works together in this function, despite included explaining commentaries...

虽然我理解在这个例子中构建的每个函数都是如何独立完成的,但是我还是不能把我的想法全部集中在这个函数中,尽管它包含了解释注释……

Can someone break it down for me, please? The function works great as is, its just driving me crazy that I don't understand how it works...

有人能帮我拆一下吗?这个功能确实很好,只是让我抓狂,我不明白它是怎么工作的……

P.S: this function is taken from Interleaving multiple arrays into a single array question.

P。S:这个函数是从将多个数组插入到单个数组问题中来的。

2 个解决方案

#1


2  

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

数组$a、$b和$c分别有10、6和2个元素。

$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,

当您将数组作为array_zip_merge()函数的参数提供时,请查看for循环。func_get_args()将使用所提供的所有参数设置$args。在开始循环运行时,

$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

在foreach循环中,array_shift将返回每个数组的第一个元素,使$输出类似

$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

在第一个for循环的末尾,array_filter函数将测试任何数组是否为空,并从$args中删除它。在第二次运行时也会发生同样的事情,在第二个for循环结束时,变量会是这样的

$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

因此,在for循环计数($args)的第三次迭代中将返回2。当$b的最后一个元素被array_shift删除时,count($args)将返回1。迭代将继续,直到所有数组都为空

#2


0  

Inside array_zip_merge, the for statement always takes the first values of each array and add them to output variable respectively.

在array_zip_merge中,for语句总是获取每个数组的第一个值,并分别将它们添加到输出变量中。

Because array_shift removes the element it returns, on every loop the first elements are different. When it gets empty because of it, the loop has nothing to do and breaks.

因为array_shift删除它返回的元素,所以在每个循环中,第一个元素是不同的。当循环因为它而变成空时,循环就没有什么可做的了,循环就会中断。

If you still dont understand, ask the specific part of the code you have trouble with please.

如果你还是不明白,请询问你有问题的代码的具体部分。

#1


2  

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

数组$a、$b和$c分别有10、6和2个元素。

$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,

当您将数组作为array_zip_merge()函数的参数提供时,请查看for循环。func_get_args()将使用所提供的所有参数设置$args。在开始循环运行时,

$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

在foreach循环中,array_shift将返回每个数组的第一个元素,使$输出类似

$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

在第一个for循环的末尾,array_filter函数将测试任何数组是否为空,并从$args中删除它。在第二次运行时也会发生同样的事情,在第二个for循环结束时,变量会是这样的

$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

因此,在for循环计数($args)的第三次迭代中将返回2。当$b的最后一个元素被array_shift删除时,count($args)将返回1。迭代将继续,直到所有数组都为空

#2


0  

Inside array_zip_merge, the for statement always takes the first values of each array and add them to output variable respectively.

在array_zip_merge中,for语句总是获取每个数组的第一个值,并分别将它们添加到输出变量中。

Because array_shift removes the element it returns, on every loop the first elements are different. When it gets empty because of it, the loop has nothing to do and breaks.

因为array_shift删除它返回的元素,所以在每个循环中,第一个元素是不同的。当循环因为它而变成空时,循环就没有什么可做的了,循环就会中断。

If you still dont understand, ask the specific part of the code you have trouble with please.

如果你还是不明白,请询问你有问题的代码的具体部分。