Change method: 1. Directly reassign the last element of the array with the syntax "$arr[sizeof($arr)-1]=new value;"; 2. Use the array_splice() function to replace the last array The value of the element, the syntax is "array_splice($arr,-1,1,"new value")".
The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
php changes the array at the end A value
Method 1: Directly reassign the last element of the array ($arr[array length-1]
)
<?php $arr=array("red","green","blue","yellow"); var_dump($arr); $arr[sizeof($arr)-1]=1; var_dump($arr); ?>
Method 2: Use the array_splice() function to replace the value of the last array element
array_splice() function from the array Removes the selected element and replaces it with a new element. The function will also return an array of removed elements.
<?php header("Content-type:text/html;charset=utf-8"); $arr=array("red","green","blue","yellow"); var_dump($arr); array_splice($arr, -1, 1, "a"); var_dump($arr); ?>
Recommended learning: "PHP Video Tutorial", "PHP ARRAY"
The above is the detailed content of How to change the value of the last element of an array in php. For more information, please follow other related articles on the PHP Chinese website!

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

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

Hot Article

Hot Tools

Atom editor mac version download
The most popular open source editor

Dreamweaver Mac version
Visual web development tools

SublimeText3 Chinese version
Chinese version, very easy to use

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 English version
Recommended: Win version, supports code prompts!
