[TASK] Differentiate between associative and nonaccociative arrays when mapping to object
This commit is contained in:
@@ -22,7 +22,11 @@ class ArrayUtility
|
||||
*/
|
||||
public static function toObject($array) {
|
||||
if (is_array($array)) {
|
||||
return (object) array_map([__CLASS__, __METHOD__], $array);
|
||||
if (self::isAssoc($array)) {
|
||||
return (object) array_map([__CLASS__, __METHOD__], $array);
|
||||
} else {
|
||||
return array_map([__CLASS__, __METHOD__], $array);
|
||||
}
|
||||
} else {
|
||||
return $array;
|
||||
}
|
||||
@@ -46,4 +50,15 @@ class ArrayUtility
|
||||
return $array;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public static function isAssoc(array $arr) {
|
||||
if (array() === $arr) {
|
||||
return false;
|
||||
}
|
||||
return array_keys($arr) !== range(0, count($arr) - 1);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user