Home >Backend Development >PHP Tutorial >Practical PHP array functions to delete array elements_PHP tutorial

Practical PHP array functions to delete array elements_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:50:47837browse

/**
* Remove blank elements from the array (including elements with only blank characters)
*
* @param array $arr
* @param boolean $trim
*/

复制代码 代码如下:

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
*/
复制代码 代码如下:

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

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/319294.htmlTechArticle/*** Remove blank elements from the array (including elements with only blank characters) * * @param array $arr * @param boolean $trim*/ 复制代码 代码如下: function array_remove_empty...
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