-1

Is there a native PHP function to zip merge two arrays?

Look at the following example:

$a = array("a","b","c");
$b = array("d","e","f");
$c = array("g","h","i");

var_dump(array_merge($a,$b,$c));

This produces:

array(9) {
  [0]=>
  string(1) "a"
  [1]=>
  string(1) "b"
  [2]=>
  string(1) "c"
  [3]=>
  string(1) "d"
  [4]=>
  string(1) "e"
  [5]=>
  string(1) "f"
  [6]=>
  string(1) "g"
  [7]=>
  string(1) "h"
  [8]=>
  string(1) "i"
}

However I want:

array(9) {
  [0]=>
  string(1) "a"
  [1]=>
  string(1) "d"
  [2]=>
  string(1) "g"
  [3]=>
  string(1) "b"
  [4]=>
  string(1) "e"
  [5]=>
  string(1) "h"
  [6]=>
  string(1) "c"
  [7]=>
  string(1) "f"
  [8]=>
  string(1) "i"
}

Therfore I wrote my own - tested an working - function:

function array_zip(...$arrays) {
    $res = array();

    while(true) {
        $check_finish = true;
        foreach($arrays as $array) {
            if(!empty($array)) {
                $check_finish = false;
            }
        }

        if($check_finish) {
            break;
        } else {

            foreach($arrays as $key => $array) {
                if(!empty($array)) {
                    array_push($res,array_shift($array)); 
                    $arrays[$key] = $array;
                }
            }
        }
    }

    return $res;
}

However is there a native PHP function to merge arrays like this (maybe more performant)? And is there a native PHP function for this purpose which preserves keys but keeps the order? Did not find sth :-/

mickmackusa
  • 43,625
  • 12
  • 83
  • 136
Blackbam
  • 17,496
  • 26
  • 97
  • 150
  • 2
    A nice little one-liner for you: `var_dump(array_merge(...array_map(null, ...[$a,$b,$c])));` requires PHP >= 5.6.0 – Mark Baker Apr 25 '17 at 18:51
  • 1
    See: http://stackoverflow.com/q/797251/3933332 for some ideas – Rizier123 Apr 25 '17 at 19:02
  • If [Transposing multidimensional arrays in PHP](https://stackoverflow.com/q/797251/2943403) and [How to Flatten a Multidimensional Array?](https://stackoverflow.com/q/1319903/2943403) had a baby, it would be this page. In other words, this is the combination of two very well established canonical pages on Stack Overflow. – mickmackusa Aug 30 '21 at 02:21

1 Answers1

0

There is no PHP native function for this purpose. However according to the comment of @Mark Baker there is a short possibility to implement this:

$a = array("a","b","c");
$b = array("d","e","f");
$c = array("g","h","i");

function array_zip(...$arrays) {
    return array_merge(...array_map(null, ...$arrays));
}

var_dump(array_zip($a,$b,$c));
Blackbam
  • 17,496
  • 26
  • 97
  • 150
  • Here is a different version of the same thing: `function array_zip() { return array_merge(...array_map(null, ...func_get_args())); }` ...though I'd just use your way in my own project. ...just thought I'd mention it. Also, if you remove the custom function call, you can reduce the number of spread operators used. `var_dump(array_merge(...array_map(null, $a, $b, $c)));` – mickmackusa Aug 30 '21 at 02:18