Home >Backend Development >PHP Tutorial >php array practical PHP array function to delete array elements

php array practical PHP array function to delete array elements

WBOY
WBOYOriginal
2016-07-29 08:38:331164browse

/**
* 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;
}
}
}
}


/**
* Convert a two-dimensional array to a hashmap
*
* If the $valueField parameter is omitted, each item of the conversion result will be an array containing all the data of the item.
*
* @param array $arr
* @param string $keyField
* @param string $valueField
*
* @return array
*/

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.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn