PHP array adding and deleting elements program code_PHP tutorial

WBOY
Release: 2016-07-13 10:43:57
Original
829 people have browsed it

There are many ways to delete and add array elements in PHP. The editor below will summarize some practical and commonly used examples of adding and deleting array elements.

There are functions to add data elements, array_push(), array_unshift() function

1. 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);
?>
输出:
Array
(
    [0] => orange
    [1] => banana
    [2] => apple
    [3] => raspberry
)

2.$arr[]

使用方法

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

$stack = array("orange", "banana");

Array_push($stack, "apple", "raspberry");

Print_r($stack);

?>

Output:

Array

(

[0] => orange

[1] => banana

[2] => apple
 代码如下 复制代码

$queue = array("orange", "banana");
array_unshift($queue, "apple", "raspberry");
print_r($queue);
?>

输出

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

[3] => raspberry

)

2.$arr[]

How to use
 代码如下 复制代码


$arr = array('a','b','c','d');
unset($arr[1]);
print_r($arr);
?> 
 
print_r($arr)

$arr = array("orange", "banana");

$arr[]='apple';

Print_r($arr);

?>

 代码如下 复制代码

$arr = array('a','b','c','d');
array_splice($arr,1,1);
print_r($arr);
?>

print_r($arr)之后,结果是Array ( [0] => a [1] => c [2] => d )

The effects of these two are the same Note: If you use array_push() to add a unit to the array, it is better to use $array[] = because there is no additional burden of calling the function. 2. Insert elements at the beginning of the 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 )
Delete the array element unset, or set it to empty directly If you want to delete an element in an array, you can directly use unset, but what I saw today surprised 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 like that. The final result is Array ( [0] => a [2] => c [3] => d ) So how can the missing elements be filled and the array 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); <🎜> ?> After print_r($arr), the result is Array ( [0] => a [1] => c [2] => d )

Delete the specified element from the array

For example, the array_slice() function takes out a segment of value from the array based on conditions and returns.

array_slice(array,offset,length,preserve)

array: array

offset: Specifies the starting position of the element to be retrieved. If it is a positive number, it is taken from the front to the back. If it is a negative value, the offset absolute value is taken from the back to the front.

The code is as follows
 代码如下 复制代码

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

输出

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

Copy code

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

Output

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

 代码如下 复制代码


$array = array('1', '2', '3', '4', '5');

$del_value = 3;
unset($array[array_search($del_value , $array)]);//利用unset删除这个元素

print_r($array);

输出

array('1', '2', '4', '5');

There is also the array_shift() function that 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.

The array_search() function is the same as in_array(), searching for a key value in the array. If the value is found, the key of the matching element is returned. If not found, return false
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.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/633138.htmlTechArticleThere are many ways to delete array elements and add array elements in php. The editor below will summarize it for you. Some practical and commonly used examples of adding and deleting array elements. Add data element...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template