PHP array Add and remove elements program code _php tutorial

Source: Internet
Author: User
There are many ways to delete an array element in PHP and to add an array element, the following is a small section to summarize some of the practical and commonly used array elements to increase the deletion instance.

Add data element with function, Array_push (), Array_unshift () function

One, add elements at the end of the array

1.array_push

How to use

The code is as follows Copy Code

$stack = Array ("Orange", "banana");
Array_push ($stack, "apple", "raspberry");
Print_r ($stack);
?>
Output:
Array
(
[0] = Orange
[1] = Banana
[2] = Apple
[3] = Raspberry
)

2. $arr []

How to use

$arr = Array ("Orange", "banana");
$arr []= ' Apple ';
Print_r ($arr);
?>

These two effects are the same.

Note: If you use Array_push () to add a cell to an array, you might as well use $array [] =, because there is no additional burden of calling the function.

Inserting elements at the beginning of an array

1.array_unshift

How to use

The code is as follows Copy Code

$queue = Array ("Orange", "banana");
Array_unshift ($queue, "apple", "raspberry");
Print_r ($queue);
?>

Output

Array
(
[0] = Apple
[1] = Raspberry
[2] = orange
[3] = Banana
)

Deletes an array element unset, or directly sets an empty


If you want to delete an element in an array, you can use the unset directly, but what you see today is a surprise to me.

The code is as follows Copy Code


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

Print_r ($arr)

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

So how can the missing elements be filled and the array will be re-indexed? The answer is Array_splice ():

The code is as follows Copy Code

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

Print_r ($arr), the result is an array ([0] = a [1] = c [2] + D)

To delete an array-specified element

such as the Array_slice () function takes a value out of a condition in the array and returns it.

Array_slice (Array,offset,length,preserve)

Array: Arrays

Offset: Specifies the starting position of the element to be removed. If it is a positive number, start with the previous one, and if it is negative, take the offset from the back to the absolute value.

The code is as follows Copy Code

$a =array (0=> "Dog",1=> "Cat",2=> "Horse",3=> "Bird");
Print_r (Array_slice ($a,));
?>

Output

Array ([0] = Cat [1] = Horse)

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.

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/633138.html www.bkjia.com true http://www.bkjia.com/PHPjc/633138.html techarticle There are many ways to delete an array element in PHP and to add an array element, the following is a small section to summarize some of the practical and commonly used array elements to increase the deletion instance. Add Data Element ...

  • 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.