Php methods for deleting array elements

Source: Internet
Author: User
Tags foreach

Instance 1

The code is as follows: Copy code


// Delete an element from the array
Function array_remove_value (& $ arr, $ var ){
Foreach ($ arr as $ key => $ value ){
If (is_array ($ value )){
Array_remove_value ($ arr [$ key], $ var );
} Else {
$ Value = trim ($ value );
If ($ value = $ var ){
Unset ($ arr [$ key]);
} Else {
$ Arr [$ key] = $ value;
}
}
}
}

Instance 2

Array_flip () deletes repeated array elements


For example:

The code is as follows: Copy code

$ Arr1 = array ("age" => 30, "name" => "Happy Park", "age" => 20 );
$ Arr1 = array_flip ($ arr1); // $ arr1 is changed to array ("Fast Park" => "name", 20 => "age ");
// Restore the key name and value of $ arr1:
$ Arr1 = array_flip ($ arr1 );

The code above is concise:

$ Arr1 = array_flip ($ arr1 ));


Instance 3

Array_pop deletes the last element of the array.

The code is as follows: Copy code

$ User = array ('apple', 'banana ', 'Orange ');
$ Result = array_pop ($ user );
Print_r ($ result );
Print_r ($ user );

Delete the element of the specified value in the array


I. Use the foreach and unset () functions to delete specific elements in the array

The code is as follows: Copy code

Foreach ($ array as $ k => $ v ){
If ($ v = 'day '){
Unset ($ array [$ k]):
}
}

The unset () function deletes the specified array value.

2. Use the array_flip () and unset () functions to delete specific values in the array.

The code is as follows: Copy code

$ Arr = array_flip ($ arr );
Unset ($ arr ['world']);
$ Arr = array_flip ($ arr );
Print_r ($ arr );

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

11.11 Big Sale for Cloud

Get Unbeatable Offers with up to 90% Off,Oct.24-Nov.13 (UTC+8)

Get It Now >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.