Practical PHP array function for deleting array elements _ PHP-php Tutorial
Source: Internet
Author: User
Php removes blank elements from an array (including elements with only blank characters) and converts a two-dimensional array to hashmap /**
* Remove blank elements from the array (including elements with only white spaces)
*
* @ Param array $ arr
* @ Param boolean $ trim
*/
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 in the conversion result is an array containing all the data of the item.
*
* @ Param array $ arr
* @ Param string $ keyField
* @ Param string $ valueField
*
* @ Return array
*/
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 content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service