Created
February 8, 2017 06:49
-
-
Save sany2k8/5a8af653730d3adb0c6bd3e9596fd3ec to your computer and use it in GitHub Desktop.
Convert a 2-D array to 1-D array
This file contains 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>'; | |
$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>'; | |
$oneDimensionalArray = array_map('current', $array); | |
print '<pre>'; | |
print_r($result); | |
print '</pre>'; | |
$oneDimensionalArray = array_column($array); | |
print '<pre>'; | |
print_r($oneDimensionalArray); | |
print '</pre>'; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment