Skip to content

Instantly share code, notes, and snippets.

@chernyshev
Created July 15, 2015 08:24
Show Gist options
  • Select an option

  • Save chernyshev/2857e08ed3bf53d7b514 to your computer and use it in GitHub Desktop.

Select an option

Save chernyshev/2857e08ed3bf53d7b514 to your computer and use it in GitHub Desktop.
array transpose with php
function transpose($array) {
    array_unshift($array, null);
    return call_user_func_array('array_map', $array);
}

NULL is given as the parameter to array_unshift, which adds a value to the start of the array. So, the first line inserts NULL as the first value of the array. The next line calls array_map with all the entries of $array as the parameters. So it's the same as calling array_map(NULL, $array[0], $array[1], $array[2], etc etc). In the array_map documentation there's a detail: "An interesting use of this function is to construct an array of arrays, which can be easily performed by using NULL as the name of the callback function"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment