From f45cf75407deb15bf5426c397c6d267839f91532 Mon Sep 17 00:00:00 2001 From: Andreas Gerhards Date: Mon, 18 Jun 2018 08:45:22 +1200 Subject: [PATCH 1/3] Implemented improved associative array check --- src/ORM/ArrayLib.php | 20 +++++++------------- 1 file changed, 7 insertions(+), 13 deletions(-) diff --git a/src/ORM/ArrayLib.php b/src/ORM/ArrayLib.php index 5f83be629..572147771 100644 --- a/src/ORM/ArrayLib.php +++ b/src/ORM/ArrayLib.php @@ -123,25 +123,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; } /** From 1753bb2d07edca24f823711f23b3f1cd912cb561 Mon Sep 17 00:00:00 2001 From: Andreas Gerhards Date: Mon, 18 Jun 2018 10:37:20 +1200 Subject: [PATCH 2/3] Improved speed on the recursive array flattening --- src/ORM/ArrayLib.php | 24 +++++++++--------------- 1 file changed, 9 insertions(+), 15 deletions(-) diff --git a/src/ORM/ArrayLib.php b/src/ORM/ArrayLib.php index 572147771..9f42e1844 100644 --- a/src/ORM/ArrayLib.php +++ b/src/ORM/ArrayLib.php @@ -75,28 +75,22 @@ class ArrayLib } /** - * @todo Improve documentation + * Flattens a nested array to a one level array * - * @param array $arr + * @param array $array * @return array */ - public static function array_values_recursive($arr) + public static function array_values_recursive($array) { - $lst = array(); + $valuesArray = []; - 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) - ); + array_walk_recursive($array, function($value, $key) use (&$valuesArray) { + if (is_scalar($value)) { + array_push($valuesArray, $value); } - } + }); - return $lst; + return $valuesArray; } /** From 59fe36ba0450f71277d8405230ee1196c6f734f9 Mon Sep 17 00:00:00 2001 From: Andreas Gerhards Date: Mon, 18 Jun 2018 11:16:20 +1200 Subject: [PATCH 3/3] Tweaked the other flattening method as well, allowing keys to be preserved --- src/ORM/ArrayLib.php | 29 +++++++++++------------------ 1 file changed, 11 insertions(+), 18 deletions(-) diff --git a/src/ORM/ArrayLib.php b/src/ORM/ArrayLib.php index 9f42e1844..7396704ac 100644 --- a/src/ORM/ArrayLib.php +++ b/src/ORM/ArrayLib.php @@ -75,22 +75,14 @@ class ArrayLib } /** - * Flattens a nested array to a one level array + * Flattens a multi-dimensional array to a one level array without preserving the keys * * @param array $array * @return array */ public static function array_values_recursive($array) { - $valuesArray = []; - - array_walk_recursive($array, function($value, $key) use (&$valuesArray) { - if (is_scalar($value)) { - array_push($valuesArray, $value); - } - }); - - return $valuesArray; + return self::flatten($array, false); } /** @@ -240,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; }