mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
Merge pull request #8188 from gelysis/array-lib
Tweaks on the ArrayLib class
This commit is contained in:
commit
a7a45559f0
@ -75,28 +75,14 @@ class ArrayLib
|
||||
}
|
||||
|
||||
/**
|
||||
* @todo Improve documentation
|
||||
* Flattens a multi-dimensional array to a one level array without preserving the keys
|
||||
*
|
||||
* @param array $arr
|
||||
* @param array $array
|
||||
* @return array
|
||||
*/
|
||||
public static function array_values_recursive($arr)
|
||||
public static function array_values_recursive($array)
|
||||
{
|
||||
$lst = array();
|
||||
|
||||
foreach (array_keys($arr) as $k) {
|
||||
$v = $arr[$k];
|
||||
if (is_scalar($v)) {
|
||||
$lst[] = $v;
|
||||
} elseif (is_array($v)) {
|
||||
$lst = array_merge(
|
||||
$lst,
|
||||
self::array_values_recursive($v)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
return $lst;
|
||||
return self::flatten($array, false);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -123,25 +109,19 @@ class ArrayLib
|
||||
* Determines if an array is associative by checking for existing keys via
|
||||
* array_key_exists().
|
||||
*
|
||||
* @see http://nz.php.net/manual/en/function.is-array.php#76188
|
||||
* @see http://nz.php.net/manual/en/function.is-array.php#121692
|
||||
*
|
||||
* @param array $arr
|
||||
* @param array $array
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public static function is_associative($arr)
|
||||
public static function is_associative($array)
|
||||
{
|
||||
if (is_array($arr) && !empty($arr)) {
|
||||
for ($iterator = count($arr) - 1; $iterator; $iterator--) {
|
||||
if (!array_key_exists($iterator, $arr)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
$isAssociative = !empty($array)
|
||||
&& is_array($array)
|
||||
&& ($array !== array_values($array));
|
||||
|
||||
return !array_key_exists(0, $arr);
|
||||
}
|
||||
|
||||
return false;
|
||||
return $isAssociative;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -252,17 +232,18 @@ class ArrayLib
|
||||
*/
|
||||
public static function flatten($array, $preserveKeys = true, &$out = array())
|
||||
{
|
||||
foreach ($array as $key => $child) {
|
||||
if (is_array($child)) {
|
||||
$out = self::flatten($child, $preserveKeys, $out);
|
||||
} else {
|
||||
if ($preserveKeys) {
|
||||
$out[$key] = $child;
|
||||
array_walk_recursive(
|
||||
$array,
|
||||
function ($value, $key) use (&$out, $preserveKeys) {
|
||||
if (!is_scalar($value)) {
|
||||
// Do nothing
|
||||
} elseif ($preserveKeys) {
|
||||
$out[$key] = $value;
|
||||
} else {
|
||||
$out[] = $child;
|
||||
$out[] = $value;
|
||||
}
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
return $out;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user