Skip to content

Instantly share code, notes, and snippets.

@Modelizer
Last active May 8, 2016 13:11
Show Gist options
  • Save Modelizer/822a60929cd5fc27f72c63dcc14d52e3 to your computer and use it in GitHub Desktop.
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.
<?php
/**
* Recursively get all values from array
*
* @param array $array
* @param bool $merge maintain array dimension
* @return array
*
* @author Mohammed Mudasir <md.hyphen@gmail.com>
* @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