PHP Delete an array of element instance programs _php Tutorial

Source: Internet
Author: User
Tags pear
In PHP array of quantum mechanics a very useful array type, let me summarize some of the various methods to delete array elements, delete the elements in the array, the first idea is to use the unset function, it can be deleted, but it may be later operation of the array when the problem occurs.

Use the following example to illustrate:

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);

The output is:
4
ARR[2] is empty
Array ([0] = 0 [1] = 1 [3] = 3 [4] = =

4) from the above output can see the length of the array is normal, but the subscript remains the original, so follow the subscript to get the element will be problematic. In addition, this method is safe for arrays of key-value forms.

How to safely remove elements? You can use the Array_splice function. The purpose of Array_splice () is to delete the series of elements specified in the original array and replace them with other values (if specified), and the returned 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);

The output is:
4
Array
([0] = 0 [1] = 1 [2] = 3 [3] = 4)
Array ([0] = =


2) If you want to get an array after removing the first element, you can have two 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);

There is also the Array_shift () function to delete the first element in the array and return 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. The form is:

Mixed Array_pop (Aray target_array);

The following example removes 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 () to delete empty elements in an array


Function name: Array_filter ()
Call mode: Array_filter ($array)
Parameter description: $array is the object of the operation, we will delete the empty element in it
Instance:

The code is as follows Copy Code

$array = (' a ' + = ' abc ', ' B ' + = ' bcd ', ' c ' = ' CDE ', ' d ' and ' Def ', ' e ' = ' = ') ';
Array_filter ($array);
echo "

";
Print_r ($array);
?>
Results:
Array (

[A] = ABC

[B] = BCD

[C] = CDE

[d] = def

)


Several functions used to feel array_search () more practical

The Array_search () function, like In_array (), looks for a key value in the array. If the value is found, the key name of the matching element is returned. Returns false if not found

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 ');

http://www.bkjia.com/PHPjc/631257.html www.bkjia.com true http://www.bkjia.com/PHPjc/631257.html techarticle in PHP array quantum mechanics a very useful array type, below I summarize some of the various methods to delete array elements, delete the elements in the array, the first idea is to use the unset function, it ...

  • Related Article

    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.