Delete PHP array functions applicable to array elements

Source: Internet
Author: User
Delete array elements Practical PHP array functions php delete blank elements from the array (including elements with only white spaces) convert a two-dimensional array to hashmap this article is from: Feet home (www.jb51.net) for details, refer to: www.jb51.netarticle15522.htm *** to delete blank elements from the array (including elements with only blank characters) ** @ paramarray to delete an array element Practical PHP array function

Php removes blank elements from an array (including elements with only blank characters) and converts a two-dimensional array to a hashmap
This article from: The foot home (www.jb51.net) detailed source reference: http://www.jb51.net/article/15522.htm

**
* Remove blank elements from the array (including elements with only white spaces)
*
* @ 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 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
*/
[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;
}

Contact Us

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

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.