Php deletes the element instance program from the array.

Source: Internet
Author: User
Tags pear

The following example is used to describe:

The code is as follows: Copy code

$ Arr = array (0, 1, 2, 3, 4 );
$ Index = 2;
Unset ($ arr [$ index]);
Echo count ($ arr );

If (empty ($ arr [$ index]) {
Echo "arr [$ index] is empty ";
}

Print_r ($ arr );

Output:
4
Arr [2] is empty
Array ([0] => 0 [1] => 1 [3] => 3 [4] =>

4) from the output result above, we can see that the length of the array is normal, but the subscript remains the original, so getting the element according to the subscript will cause a problem. In addition, this method is safe for arrays in the key-value format.

 

How can we safely delete elements? You can use the array_splice function. Array_splice () is used to delete a series of elements specified in the original array and use other values instead (if specified). The return value is the deleted element.

 

The code is as follows: Copy code

$ Arr = array (0, 1, 2, 3, 4 );
$ Rtn = array_splice ($ arr, 2, 1 );
Echo count ($ arr );
Print_r ($ arr );
Print_r ($ rtn );

Output:
4
Array
([0] => 0 [1] => 1 [2] => 3 [3] => 4)
Array ([0] =>


2) If you want to obtain the array after the first element is deleted, you can use either of the following methods:

The code is as follows: Copy code

1,
$ Arr = array (0, 1, 2, 3, 4 );
Array_splice ($ arr, 0, 1 );

2,
$ Arr = array (0, 1, 2, 3, 4 );
$ Arr = array_splice ($ arr, 1 );

The array_shift () function also deletes the first element in the array and returns the value of the deleted element.

The relative array_pop () function deletes the last element in the array.

The array_pop () function deletes the array and returns the last element of the array. The format is:

Mixed array_pop (aray target_array );

The following example deletes the last state from the $ states array:

The code is as follows: Copy code

$ Fruits = array ("apple", "banana", "orange", "pear ");

$ Fruit = array_pop ($ fruits );

// $ Fruits = array ("apple", "banana", "orange ");

// $ Fruit = "pear ";

Array_filter () deletes an empty element from the array.


Function name: array_filter ()
Call method: array_filter ($ array)
Parameter description: $ array is the operation object. We will delete the null element.
Instance:

The code is as follows: Copy code

<? Php
$ Array = ('a' => "abc", 'B' => "bcd", 'C' => "cde", 'D' => "def ", 'E' => "");
Array_filter ($ array );
Echo "<pre> ";
Print_r ($ array );
?>
Result:
Array (

[A] => abc

[B] => bcd

[C] => cde

[D] => def

   )


I think array_search () is more practical when using several functions.

The array_search () function is the same as the in_array () function. You can find a key value in the array. If this value is found, the key name of the matching element is returned. If not found, false is returned.

The code is as follows: Copy code


$ Array = array ('1', '2', '3', '4', '5 ');

$ Del_value = 3;
Unset ($ array [array_search ($ del_value, $ array)]); // use unset to delete this element

Print_r ($ array );

Output

Array ('1', '2', '4', '5 ');

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.