PHP removes blank elements from the array (including elements with only blank characters) and converts a two-dimensional array into a hashmap
This article comes from: Script House ( www.jb51.net ) Detailed source reference: http://www.jb51.net/article/15522.htm
**
* Remove blank elements from the array (including elements with only blank characters)
*
* @param array $arr
* @param boolean $trim
*/
[Copy this code]CODE:
function array_remove_empty(& $arr, $trim = true)
{
foreach ($arr as $key => $value) {
if (is_array($value)) {
array_remove_empty($arr[$key]);
} else {
$value = trim($value);
if ($value == '') {
unset($arr[$key]);
} elseif ($trim) {
$arr[$key] = $value;
}
}
}
}
/**
* Convert a two-dimensional array to a hashmap
*
* If the $valueField parameter is omitted, each item of the conversion result is an array containing all the data of the item.
*
* @param array $arr
* @param string $keyField
* @param string $valueField
*
* @return array
*/
[Copy this code]CODE:
function array_to_hashmap(& $arr, $keyField, $valueField = null)
{
$ret = array();
if($valueField) {
foreach ($arr as $row) {
$ret[$row[$keyField]] = $row[$valueField];
}
} else {
foreach($arr as $row) {
$ret[$row[$keyField]] = $row;
}
}
return $ret;
}