Several methods for deleting array elements in php

Source: Internet
Author: User
There are many ways to delete array elements in php. the built-in functions of php provide us with two functions: array_slice () and array_flip (). you can refer to them. we first use the php function to implement: arr.

There are many ways to delete array elements in php. the built-in functions of php provide us with two functions: array_slice () and array_flip (). you can refer to the following:

Array_slice ()The first parameter is the array to be cut, the second parameter is the starting position, and the third parameter is the length, which is the cut $ arr array, which is counted three after 0th elements, the code is as follows:

Array ("a", "B", "c", "d", "e", "f", "g ")

$ Arr = array_slice ($ arr, 0, 3 );

Array_flip achieves de-duplication effect phpfensi ~ Com

Another method is to use the array_flip function of php to indirectly implement deduplication.

Array_flip is a function used to reverse the array key and value. it has a feature that if two values in the array are the same, the last key and value will be retained after the inversion. we use this feature to indirectly implement array deduplication. the code is as follows:

  1. $ Arr = array ("a" => "a1", "B" => 'b1 ', "c" => "a2 ", "d" => "a1 ");
  2. $ Arr1 = array_flip ($ arr );
  3. Print_r ($ arr1); // first reverse the expression, remove the duplicate value, and output Array ([a1] => d [b1] => B [a2] => c)
  4. $ Arr2 = array_flip ($ arr );
  5. Print_r ($ arr2); // returns the array after deduplication, output Array ([a1] => d [b1] => B [a2] => c)
  6. $ Arr3 = array_unique ($ arr );
  7. Print_r ($ arr3); // use the array_unique function of php to deduplicate and output Array ([a] => a1 [B] => b1 [c] => a2)
  8. ?>

The following is a commonly used method unset to delete objects. the code is as follows:

  1. $ Arr = array ('A', 'B', 'C', 'D ');
  2. Unset ($ arr [1]);
  3. Print_r ($ arr );
  4. ?>
  5. // After print_r ($ arr), the result is not the same. The final result is Array ([0] => a [2] => c [3] => d

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.