php如何删除数组的第一个元素和最后一个元素
对于一个php数组,该如何删除该数组的第一个元素或者最后一个元素呢?其实这两个过程都可以通过php自带的函数 array_pop 和 array_shift 来完成,下面就具体介绍一下如何来操作。
(1)使用 array_pop 删除数组的最后一个元素,例如:
<p>$user=array('apple','banana','orange');</p>$result=array_pop($user);<br />print_r($result);<br /><p>print_r($user);</p>
结果将是:
orange
array('apple','banana')
(2)使用 array_shift 删除数组的第一个元素,例如:
<p>$user=array('apple','banana','orange');</p>$result=array_shift($user);<br />print_r($result);<br /><p>print_r($user);</p>
结果将是:
apple
array('banana','orange')
其实删除数组的第一个元素还可以使用 array_splice 这个函数,即:
$user=array_splice($user,1); //删除数组第一个元素,注意此时返回的是被删除后的新的数组
下面简单的对 array_pop 和 array_shift 做一下解释:
array_pop() 弹出并返回 array 数组的最后一个单元,并将数组 array 的长度减一。如果 array 为空(或者不是数组)将返回 NULL。
array_shift() 将 array 的第一个单元移出并作为结果返回,将 array 的长度减一并将所有其它单元向前移动一位。所有的数字键名将改为从零开始计数,文字键名将不变。如果 array 为空(或者不是数组),则返回 NULL。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Solution to the problem that pressing shift cannot switch between Chinese and English: 1. Find the win key and click to open; 2. Find "Settings" and click to open, then open "Time and Language"; 3. Click "Region and Language" and open "Options" "; 4. Click "Options" and then select "shift" in the first line.

Recently, some friends have reported that they don’t know where to press, causing the shift key on the keyboard to be locked. What’s going on? How to solve this situation? Here I will give you a detailed introduction to Win10 You can take a look at how to unlock the keyboard shift lock. Undoing method: 1. Click "Start" in the taskbar in the lower left corner and select "Settings" in the menu list. 2. After entering the new interface, click the "Time and Language" option. 3. Then click "Region and Language" in the left column, then click "Advanced Keyboard Settings" in "Related Settings" on the right 4. Then click "Language Bar Options". 5. In the window that opens, click the "Advanced Key Settings" tab at the top. 6. Finally

ctrl+shift is a combination shortcut key for switching input methods; when pressing "ctrl+shift" at the same time, you can also hold down other keys to achieve other functions, such as pressing "ctrl+shift+N" at the same time key to create a new folder, etc.

Title: Example of using the Array.Sort function to sort an array in C# Text: In C#, array is a commonly used data structure, and it is often necessary to sort the array. C# provides the Array class, which has the Sort method to conveniently sort arrays. This article will demonstrate how to use the Array.Sort function in C# to sort an array and provide specific code examples. First, we need to understand the basic usage of the Array.Sort function. Array.So

Shift is a shift key in the keyboard, also called the keyboard shift key, located in the lower left corner of the keyboard; the shift key has functions such as switching input methods, quickly switching between half-width and full-width, selecting consecutive files, and directly deleting files.

The shift key of Mac is a key above the fn key and below the caps lock key. This key is on the far left side of the keyboard, the second key from the bottom right; the shift key is the upshift key in the keyboard, and can be accessed through " Ctrl+Shift" key combination to switch input methods.

When programming in PHP, we often need to merge arrays. PHP provides the array_merge() function to complete array merging, but when the same key exists in the array, this function will overwrite the original value. In order to solve this problem, PHP also provides an array_merge_recursive() function in the language, which can merge arrays and retain the values of the same keys, making the program design more flexible. array_merge

In Docker, the permission problem of the mounting directory can usually be solved by the following method: adding permission-related options when using the -v parameter to specify the mounting directory. You can specify the permissions of the mounted directory by adding: ro or :rw after the mounted directory, indicating read-only and read-write permissions respectively. For example: dockerrun-v/host/path:/container/path:roimage_name Define the USER directive in the Dockerfile to specify the user running in the container to ensure that operations inside the container comply with permission requirements. For example: FROMimage_name#CreateanewuserRUNuseradd-ms/bin/
