lithium\util\Set::normalize()
Normalizes a string or array list.
Set::normalize('foo,bar'); // returns array('foo' => null, 'bar' => null);
Set::normalize(array('foo', 'bar' => 'baz'); // returns array('foo' => null, 'bar' => 'baz');
Parameters
-
string|array
$list
List to normalize.
-
boolean
$assoc
If
true
,$list
will be converted to an associative array. -
string
$sep
If
$list
is a string, it will be split into an array with$sep
. -
boolean
$trim
If
true
, separated strings will be trimmed.
Returns
arraySource
public static function normalize($list, $assoc = true, $sep = ',', $trim = true) {
if (is_string($list)) {
$list = explode($sep, $list);
$list = ($trim) ? array_map('trim', $list) : $list;
return ($assoc) ? static::normalize($list) : $list;
}
if (!is_array($list)) {
return $list;
}
$keys = array_keys($list);
$count = count($keys);
$numeric = true;
if (!$assoc) {
for ($i = 0; $i < $count; $i++) {
if (!is_int($keys[$i])) {
$numeric = false;
break;
}
}
}
if (!$numeric || $assoc) {
$newList = array();
for ($i = 0; $i < $count; $i++) {
if (is_int($keys[$i]) && is_scalar($list[$keys[$i]])) {
$newList[$list[$keys[$i]]] = null;
} else {
$newList[$keys[$i]] = $list[$keys[$i]];
}
}
$list = $newList;
}
return $list;
}