search
HomeheadlinesPHP commonly used array functions (2)

* 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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.