Home >Backend Development >PHP Tutorial >php array practical PHP array function to delete array elements
/**
* Remove blank elements from the array (including elements with only blank characters)
*
* @param array $arr
* @param boolean $trim
*/
Copy code The code is as follows:
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;
}
}
}
}
Copy the code The code is as follows:
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;
}
The above introduces the practical PHP array functions for deleting array elements in php arrays, including the content of php arrays. I hope it will be helpful to friends who are interested in PHP tutorials.