Php deletes the element instance program from the array.

Source: Internet
Author: User
In php, array quantum mechanics is a very useful array type. next I will summarize some methods to delete array elements and delete elements in the array. The first idea is to use the unset function. it does

In php, array quantum mechanics is a very useful array type. next I will summarize some methods to delete array elements and delete elements in the array. The first idea is to use the unset function, it can be deleted, but it may cause problems when operating the array later.

The following example shows the code:

  1. $ Arr = array (0, 1, 2, 3, 4 );
  2. $ Index = 2;
  3. Unset ($ arr [$ index]);
  4. Echo count ($ arr );
  5. If (emptyempty ($ arr [$ index]) {
  6. Echo "arr [$ index] is empty ";
  7. }
  8. Print_r ($ arr );
  9. /*
  10. Output:
  11. 4
  12. Arr [2] is empty
  13. Array ([0] => 0 [1] => 1 [3] => 3 [4] =>
  14. */

From the output result above, we can see that the length of the array is normal, but the subscript remains the original. Therefore, an error occurs when an element is obtained according to the subscript, 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:

  1. $ Arr = array (0, 1, 2, 3, 4 );
  2. $ Rtn = array_splice ($ arr, 2, 1 );
  3. Echo count ($ arr );
  4. Print_r ($ arr );
  5. Print_r ($ rtn );
  6. /*
  7. Output:
  8. 4
  9. Array
  10. ([0] => 0 [1] => 1 [2] => 3 [3] => 4)
  11. Array ([0] =>
  12. */

To retrieve the array after the first element is deleted, you can use the following two methods:

  1. // 1,
  2. $ Arr = array (0, 1, 2, 3, 4 );
  3. Array_splice ($ arr, 0, 1 );
  4. // 2,
  5. $ Arr = array (0, 1, 2, 3, 4 );
  6. $ 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 and returns the last element of the array, in the form:

Mixed array_pop (aray target_array );

The following example deletes the last one from the $ states array. the code is as follows:

  1. $ Fruits = array ("apple", "banana", "orange", "pear ");
  2. Fruit = array_pop ($ fruits );
  3. // $ Fruits = array ("apple", "banana", "orange ");
  4. /$ 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.

The instance code is as follows:

  1. $ Array = ('a' => "abc", 'B' => "bcd", 'C' => "cde", 'D' => "def ", 'E' => "");
  2. Array_filter ($ array );
  3. Echo"
    "; 
  4. Print_r ($ array );
  5. ?>
  6. /* Result:
  7. Array (
  8. [A] => abc
  9. [B] => bcd
  10. [C] => cde
  11. [D] => def
  12. )
  13. */

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

Like the in_array () function, array_search () finds a key value in the array. if this value is found, the key name of the matching element is returned. if this value is not found, false is returned, the code is as follows:

  1. $ Array = array ('1', '2', '3', '4', '5 ');
  2. $ Del_value = 3;
  3. Unset ($ array [array_search ($ del_value, $ array)]); // use unset to delete this element
  4. Print_r ($ array );
  5. // 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.