Last active
August 15, 2018 07:41
-
-
Save ValekS/c522de1aaac2e8ab5b649a4863fb8c8a to your computer and use it in GitHub Desktop.
Example of array handling
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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