PHP Delete array elements several methods _php tutorial

Source: Internet
Author: User
There are many ways to delete array elements in PHP, PHP built-in for us to provide two functions array_slice () and Array_flip () you can refer to. Let's first use PHP functions to implement

Array_slice () The first parameter is the array to be cut, the second argument is the starting position, and the third parameter is the length.
is to cut $arr this array, from the No. 0 element back to the number of 3.

The code is as follows Copy Code

Array ("A", "B", "C", "D", "E", "F", "G")

$arr = Array_slice ($arr, 0, 3);


Array_flip to achieve de-weight effect phperz~com

Another method is to use PHP's Array_flip function to indirectly implement the redo effect.

Array_flip is a function that reverses array keys and values, and he has a feature that if two values are the same in an array, the last key and value are preserved after the reversal. Using this feature we use him to indirectly implement the weight of the array.

The code is as follows Copy Code

$arr = Array ("A" = "A1", "B" = "B1", "c" = "A2", "D" = "A1");
$arr 1 = array_flip ($arr);
Print_r ($arr 1);//Invert once, remove duplicate values, output array ([A1] + d [B1] + b [a2] = c)
$arr 2 = Array_flip ($arr);
Print_r ($arr 2);//invert back, get the array to go back, Output Array ([A1] + d [B1] = b [A2] + c)
$arr 3 = Array_unique ($arr);
Print_r ($arr 3);//use PHP's Array_unique function to go heavy, output array ([a] = a1 [b] = B1 [c] = = A2)
?>

Here is the method unset I used to implement the deletion

The code is as follows:

The code is as follows Copy Code

$arr = Array (' A ', ' B ', ' C ', ' d ');
Unset ($arr [1]);
Print_r ($arr);
?>

Print_r ($arr), the result is not that, the end result is an Array ([0] = a [2] = c [3] + D

http://www.bkjia.com/PHPjc/631614.html www.bkjia.com true http://www.bkjia.com/PHPjc/631614.html techarticle There are many ways to delete array elements in PHP, PHP built-in for us to provide two functions array_slice () and Array_flip () you can refer to. We first use 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.