解释这个自定义函数如何工作PHP [英] Explain how this custom function works PHP

查看:151
本文介绍了解释这个自定义函数如何工作PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

PHP中的这个函数允许以输出数组的方式合并任何N个不同长度的数组,数组将按照下列顺序排列: Array1 [0],Array2 [0],.., ArrayN [0],Array1 [1],Array2 [1],..,ArrayN [1] ...

 函数array_zip_merge(){
$ output = array();
//循环增量器将每个数组从循环中取出,因为它被array_shift()清空。
用于($ args = func_get_args(); count($ args); $ args = array_filter($ args)){
//& $ arg允许array_shift()更改原始值。
foreach($ args as& $ arg){
$ output [] = array_shift($ arg);
}
}
return $ output;
}

//测试

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

虽然我明白这个例子中每个内置函数都是自己做的,我只是不能把我的想法包括在这个函数中,尽管包括解释评论......它们是如何协同工作的。...

请有人为我分解它吗?这个函数效果很好,它只是让我疯狂,我不明白它是如何工作的......



PS:这个函数取自将多个数组放入单个数组问题。


<数组 $ 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'];

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

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

foreach 循环中, array_shift 将返回每个数组的第一个元素,导致 $ output 变得像

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

数组现在看起来像,

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

在循环的第一个结尾处 array_filter 函数将测试任何数组是否为空,并将它从 $ args 中移除。第二次运行也会发生同样的情况,并且在第二次结束时,循环中的变量看起来像

  $ a = [3,4,5,6,7,8,9,10]; 
$ b = ['c','d','e','f'];
$ c = [];
$ output = $ output = [1,'a','A',2,'b','B'];
//因为$ c是空的array_filter()将它从$ args中移除
$ args = [$ a,$ b];

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


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]... :

    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.

解决方案

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

这篇关于解释这个自定义函数如何工作PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆