Created
July 21, 2017 10:47
-
-
Save sany2k8/d2e95ea314a686c7691054a476b0cb07 to your computer and use it in GitHub Desktop.
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 | |
| /** | |
| * Created by PhpStorm. | |
| * User: Computer Source | |
| * Date: 12/12/14 | |
| * Time: 3:56 PM | |
| */ | |
| $array = array( | |
| array(554 => "a"), | |
| array(561 => "b"), | |
| array(574 => "c") | |
| ); | |
| $array = array( | |
| array(554 => array(1,3,2)), | |
| array(561 => array(1,9,2)), | |
| array(574 => array(5,3,2)) | |
| ); | |
| $singleDimension=array_reduce($array,function($array,$item){return $array+$item;},array()); | |
| print '<pre>'; | |
| print_r($singleDimension); | |
| print '</pre>'; | |
| // works good | |
| echo 'merge'; | |
| $singleDimension2=call_user_func_array('array_merge',$array); | |
| print '<pre>'; | |
| print_r($singleDimension2); | |
| print '</pre>'; | |
| foreach(new RecursiveIteratorIterator(new RecursiveArrayIterator($array)) as $k=>$v){ | |
| $singleDimension3[$k]=$v; | |
| } | |
| print '<pre>'; | |
| print_r($singleDimension3); | |
| print '</pre>'; | |
| $result = array(); | |
| array_walk_recursive($array,function($v, $k) use (&$result){ $result[] = $v; }); | |
| print '<pre>'; | |
| print_r($result); | |
| print '</pre>'; | |
| //keep keys, considers only the first element of every 2d array | |
| $oneDimensionalArray = array_map('current', $array); | |
| print '<pre>'; | |
| print_r($result); | |
| print '</pre>'; | |
| $oneDimensionalArray = array_column($array); | |
| print '<pre>'; | |
| print_r($oneDimensionalArray); | |
| print '</pre>'; | |
| // PHP > 5.6: | |
| $data = [[1, 2], [3], [4, 5]]; | |
| print_r(array_merge(... $data)); // [1, 2, 3, 4, 5]; | |
| //PHP < 5.6: | |
| $data = [[1, 2], [3], [4, 5]]; | |
| print_r(array_reduce($data, 'array_merge', [])); // [1, 2, 3, 4, 5]; | |
| ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment