Home >headlines >PHP commonly used array functions (2)

PHP commonly used array functions (2)

无忌哥哥
无忌哥哥Original
2018-06-28 10:25:261769browse

* Stacks and queues are the two most commonly used data structures. As for what stacks and queues are, it is beyond the course requirements.

* All you need to know now is that stacks and queues can quickly add elements to both ends of an array. The addition and deletion operations can be done

* The stack operation of the array: the addition and deletion of elements are only allowed at one end of the array

* 1. The tail

* 1. array_push(array, value1[,value2...): Push to the stack, return the new array number

* 2. array_pop(array): Pop the last element from the stack, the array length is reduced by 1

* 2. Header

* 1. array_unshift(array,value1[,value2...): Push to the stack and return the number of new arrays

* 2. array_shift(array): Pop the stack, pop the first element at the head, and the array length is reduced by 1

* Queue operation of the array: the addition and deletion of elements are allowed at both ends

* 1. Enter the tail into the queue, Head dequeue

* 1. array_push(array, value1[,value2...):Enqueue, return the number of new arrays

* 2. array_shift(array):Dequeue, Pop the first element in the head, and the array length is reduced by 1

* 2. The head is added to the queue, and the tail is dequeued

* 1. array_unshift(array,value1[,value2...) :Enqueue, return the new array number

* 2. array_pop(array): Dequeue, pop the last element, the array length is reduced by 1

* Note:

* 1. Added elements always appear as index elements, and multiple

can be added at the same time. * 2. Deletion can only pop up one element at a time.

* 3. Addition and deletion operations will cause the array pointer to be The reset operation reset()

echo &#39;<pre class="brush:php;toolbar:false">&#39;;
$user = [&#39;id&#39;=>5,&#39;name&#39;=>&#39;peter&#39;,&#39;gender&#39;=>&#39;male&#39;,&#39;age&#39;=>30];
print_r($user); //查看数组
echo &#39;<hr color="red">&#39;;

//First, simulate the stack operation: addition and deletion of elements are only allowed at one end

//1. array_push(array, value1[,value2.. .): Push the tail onto the stack, return the new array number

echo array_push($user, &#39;php中文网&#39;,&#39;www.php.cn&#39;),&#39;<br>&#39;;
print_r($user); //查看新成的数组

//2. array_pop(array): Pop the tail off the stack, pop the last element, and reduce the array length by 1

echo array_pop($user),&#39;<br>&#39;;
print_r($user); //查看新成的数组

//Repeat An element is popped from the tail and has been restored to its original state

echo array_pop($user),&#39;<br>&#39;;
print_r($user); //查看新成的数组

//3. array_unshift(array, value1[, value2...): Push the head onto the stack and return the new array number

echo array_unshift($user, &#39;php中文网&#39;,&#39;www.php.cn&#39;),&#39;<br>&#39;;
print_r($user); //查看新成的数组

//4. array_shift(array): Pop the head from the stack, pop the first element of the head, and reduce the array length by 1

echo array_shift($user),&#39;<br>&#39;;
print_r($user); //查看新成的数组
echo array_shift($user),&#39;<br>&#39;;
print_r($user); //查看新成的数组

//Second: Simulate queue operation: additions and deletions must be done at both ends , not allowed to be completed at the same end

// 1. array_push(array, value1[,value2...): The tail is added to the queue and the new array number is returned

echo array_push($user, &#39;php中文网&#39;,&#39;www.php.cn&#39;),&#39;<br>&#39;;
print_r($user); //查看新成的数组

// 2. array_shift (array): The head is dequeued, the first element of the head is popped out, and the array length is reduced by 1

echo array_shift($user),&#39;<br>&#39;;  //出队的id=5这个元素,当然返回的只有值5
print_r($user); //查看新成的数组

// 3. array_unshift(array,value1[,value2...): The head is put into the queue, Return the number of new arrays

echo array_unshift($user, &#39;华为&#39;,&#39;小米&#39;),&#39;<br>&#39;;
print_r($user); //查看新成的数组

// 4. array_pop(array): dequeue the tail, pop the last element, and reduce the array length by 1

echo array_pop($user),&#39;<br>&#39;;
print_r($user); //查看新成的数组
Statement:
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