Php array add and delete element program code

Source: Internet
Author: User

There are many ways to delete array elements and add array elements in php. The following section will summarize some practical and common methods to add and delete instances for you.

Added the data element functions, including array_push () and array_unshift ().

1. Add elements at the end of the array

1. array_push

Usage

The Code is as follows: Copy code

<? Php
$ Stack = array ("orange", "banana ");
Array_push ($ stack, "apple", "raspberry ");
Print_r ($ stack );
?>
Output:
Array
(
[0] => orange
[1] => banana
[2] => apple
[3] => raspberry
)

2. $ arr []

Usage

<? Php

$ Arr = array ("orange", "banana ");
$ Arr [] = 'apple ';
Print_r ($ arr );
?>

The two effects are the same.

NOTE: If array_push () is used to add a unit to the array, it is better to use $ array [] =, because there is no additional burden to call the function.

2. Insert elements at the beginning of the array

1. array_unshift

Usage

The Code is as follows: Copy code

<? Php
$ Queue = array ("orange", "banana ");
Array_unshift ($ queue, "apple", "raspberry ");
Print_r ($ queue );
?>

Output

Array
(
[0] => apple
[1] => raspberry
[2] => orange
[3] => banana
)

Delete the array element unset, or directly set null


If you want to delete an element from an array, you can use the unset directly, but what you see today surprised me.

The Code is as follows: Copy code


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

The result is not that. The final result is Array ([0] => a [2] => c [3] => d)

 

So how can we fill in the missing elements and re-index the array? The answer is array_splice ():

The Code is as follows: Copy code

<? Php
$ Arr = array ('A', 'B', 'C', 'D ');
Array_splice ($ arr, 1, 1 );
Print_r ($ arr );
?>

After print_r ($ arr), the result is Array ([0] => a [1] => c [2] => d)

Deletes the specified element of an array.

For example, the array_slice () function extracts a value from the Array Based on the Conditions and returns the result.

Array_slice (array, offset, length, preserve)

Array: array

Offset: Specifies the start position of the retrieved element. If it is a positive number, it is taken from the beginning to the end. If it is a negative value, it is taken from the back to the absolute value of the offset.

 

The Code is as follows: Copy code

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

Output

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

The array_shift () function also deletes the first element in the array and returns the value of the deleted element.

The relative array_pop () function deletes the last element in the array.

I think array_search () is more practical when using several functions.

The array_search () function is the same as the in_array () function. You can find a key value in the array. If this value is found, the key name of the matching element is returned. If not found, false is returned.

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

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.