Last active
May 8, 2016 13:11
-
-
Save Modelizer/822a60929cd5fc27f72c63dcc14d52e3 to your computer and use it in GitHub Desktop.
Get all values from array no matter array is multi dimension. Also you can maintain array dimension if you need.
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 | |
/** | |
* Recursively get all values from array | |
* | |
* @param array $array | |
* @param bool $merge maintain array dimension | |
* @return array | |
* | |
* @author Mohammed Mudasir <[email protected]> | |
* @license MIT | |
*/ | |
function array_recursive_value(array $array, $merge = true) | |
{ | |
$values = array(); | |
foreach ($array as $key => $value) { | |
$values[] = is_array($value) ? array_recursive_value($value, $merge) : $value; | |
} | |
if (! $merge) { | |
return $values; | |
} | |
// Get values for merge if value is string then make it as array with first | |
// pocket will be string. | |
$valuesForMerge = function () use ($values) { | |
return array_map(function ($value) { | |
return is_array($value) ? $value : [$value]; | |
}, $values); | |
}; | |
return call_user_func_array('array_merge', $valuesForMerge()); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment