Explain how this custom function works PHP

随声附和 提交于 2019-12-19 17:46:38

问题


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.


回答1:


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




回答2:


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

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.

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



来源:https://stackoverflow.com/questions/34482028/explain-how-this-custom-function-works-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!