Skip to content

Instantly share code, notes, and snippets.

@ValekS
Last active August 15, 2018 07:41
Show Gist options
  • Save ValekS/c522de1aaac2e8ab5b649a4863fb8c8a to your computer and use it in GitHub Desktop.
Save ValekS/c522de1aaac2e8ab5b649a4863fb8c8a to your computer and use it in GitHub Desktop.
Example of array handling
<?php
/**
* [
* [1, 1],
* [1, 2],
* [1, 3],
* etc.
* ]
* to
* [1 => [
* 1,
* 2,
* 3,
* etc.
* ]]
*/
$array = [[8, 2], [8, 3], [8, 5], [8, 8]];
$result = [];
array_walk($array, function ($item, $key) use (&$result) {
$result[array_shift($item)][] = current($item); // or $item[0]
});
print_r($result);
// See DEMO: http://sandbox.onlinephpfunctions.com/code/453cc24d652fc0dc453a8feb13f16be5135b58cb
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment