How to replace value in php array
In PHP, you can use the array_splice() function to replace the value in the array. This function can remove the selected element from the array and replace it with a new element; the syntax format is "array_splice(array, The position to start deletion, the number of deleted elements, and the replacement value)".
The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
In php, you can use array_splice () function to replace the value value in the array.
Example: Replace value value in array
<?php $a1=array("a"=>"red","b"=>"green","c"=>"blue","d"=>"yellow"); $a2=array("a"=>"purple","b"=>"orange"); array_splice($a1,0,2,$a2); print_r($a1); ?>
Output:
Array ( [0] => purple [1] => orange [c] => blue [d] => yellow )
Explanation:
array_splice() function is used to delete part of the elements of the array; you can delete them directly or replace them with other values.
If the function does not remove any elements (length=0), the replacement array will be inserted from the position of the start parameter
array_splice() The syntax is as follows:
array_splice(array,start,length,replacement)
Parameter description :
array represents an array.
start indicates the position (subscript) where deletion starts:
If start is a positive number, delete from front to back.
If start is a negative number, start from the position -start from the end of arr and delete from back to front. For example -2 means start from the second to last element of the array.
length is an optional parameter, indicating the number of elements to delete:
If length is a positive number, then It means deleting length elements;
If length is a negative number, then all elements starting from start and counting down to length from the end of the array will be deleted;
-
If omitted, all elements from start to the end of the array will be deleted.
#replacement is an optional parameter indicating the value to be replaced. If replacement has multiple values, it needs to be set to an array. If there is only one value, it does not need to be set to an array.
If the combination of start and length results in no element being deleted, then the value contained in replacement will be inserted into the position specified by start.
Note that using replacement to replace array elements will not retain the original key names.
Return value: Returns an array consisting of the deleted elements.
Example:
<?php $arr = array("red", "green", "blue", "yellow"); array_splice($arr, 2); print_r($arr); //$arr 现在变成 array("red", "green") $arr = array("red", "green", "blue", "yellow"); array_splice($arr, 1, -1); print_r($arr); //$arr 现在变成 array("red", "yellow") $arr = array("red", "green", "blue", "yellow"); array_splice($arr, 1, count($arr), "orange"); print_r($arr); //$arr 现在变成 array("red", "orange") $arr = array("red", "green", "blue", "yellow"); array_splice($arr, -1, 1, array("black", "maroon")); print_r($arr); //$input 现在变成 array("red", "green", "blue", "black", "maroon") $arr = array("red", "green", "blue", "yellow"); array_splice($arr, 3, 0, "purple"); print_r($arr); //$arr 现在变成 array("red", "green", "blue", "purple", "yellow"); ?>
Output:
Array ( [0] => red [1] => green ) Array ( [0] => red [1] => yellow ) Array ( [0] => red [1] => orange ) Array ( [0] => red [1] => green [2] => blue [3] => black [4] => maroon ) Array ( [0] => red [1] => green [2] => blue [3] => purple [4] => yellow )
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of How to replace value in php array. 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

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



PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

To work on file upload we are going to use the form helper. Here, is an example for file upload.

Validator can be created by adding the following two lines in the controller.

Logging in CakePHP is a very easy task. You just have to use one function. You can log errors, exceptions, user activities, action taken by users, for any background process like cronjob. Logging data in CakePHP is easy. The log() function is provide

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

CakePHP is an open source MVC framework. It makes developing, deploying and maintaining applications much easier. CakePHP has a number of libraries to reduce the overload of most common tasks.
