fork download
  1. <?php
  2.  
  3. function zip() {
  4. $params = func_get_args();
  5. if (count($params) === 1){
  6. // single iterable passed
  7. $result = array();
  8. foreach ($params[0] as $item){
  9. $result[] = array($item);
  10. };
  11. return $result;
  12. };
  13. $result = call_user_func_array('array_map', array_merge(array(null), $params));
  14. $length = min(array_map('count', $params));
  15. return array_slice($result, 0, $length);
  16. };
  17.  
  18. $a1 = array(1,2,3,4,5);
  19. $a2 = array('a','b');
  20. $a3 = array('x','y','z');
  21.  
  22. $b1 = $a1;
  23. $b2 = $a2;
  24.  
  25. $c1 = array(1,2,3,4,5);
  26.  
  27. echo "\n== Test 1 ==\n";
  28. var_dump(zip($a1, $a2, $a3));
  29. echo "\n== Test 2 ==\n";
  30. var_dump(zip($b1, $b2));
  31. echo "\n== Test 3 ==\n";
  32. var_dump(zip($c1));
Success #stdin #stdout 0.02s 13064KB
stdin
Standard input is empty
stdout
== Test 1 ==
array(2) {
  [0]=>
  array(3) {
    [0]=>
    int(1)
    [1]=>
    string(1) "a"
    [2]=>
    string(1) "x"
  }
  [1]=>
  array(3) {
    [0]=>
    int(2)
    [1]=>
    string(1) "b"
    [2]=>
    string(1) "y"
  }
}

== Test 2 ==
array(2) {
  [0]=>
  array(2) {
    [0]=>
    int(1)
    [1]=>
    string(1) "a"
  }
  [1]=>
  array(2) {
    [0]=>
    int(2)
    [1]=>
    string(1) "b"
  }
}

== Test 3 ==
array(5) {
  [0]=>
  array(1) {
    [0]=>
    int(1)
  }
  [1]=>
  array(1) {
    [0]=>
    int(2)
  }
  [2]=>
  array(1) {
    [0]=>
    int(3)
  }
  [3]=>
  array(1) {
    [0]=>
    int(4)
  }
  [4]=>
  array(1) {
    [0]=>
    int(5)
  }
}