Php: How to delete array elements _ PHP Tutorial

Source: Internet
Author: User
Php deletes array elements. There are many ways to delete array elements in php. php provides two built-in functions: array_slice () and array_flip. We first use the php function to implement array_s. There are many ways to delete array elements in php. the built-in php provides two functions: array_slice () and array_flip (). you can refer to them. We first use the php function to implement

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.
It is to cut the $ arr array and count it three times from 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 phperz ~ 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:

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

The following is a common method unset to delete objects.

The code is as follows:

The code is as follows:

$ Arr = array ('A', 'B', 'C', 'D ');
Unset ($ arr [1]);
Print_r ($ arr );
?>

After print_r ($ arr), the result is not that. The final result is Array ([0] => a [2] => c [3] => d

For more information about lift () and array_flip (), see. We first use the php function to implement array_s...

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.