Home Backend Development PHP Problem How to replace value in php array

How to replace value in php array

Jul 15, 2021 am 10:15 AM
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)".

How to replace value in php array

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);
?>
Copy after login

Output:

Array ( [0] => purple [1] => orange [c] => blue [d] => yellow )
Copy after login

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)
Copy after login

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");
?>
Copy after login

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
)
Copy after login

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!

Statement of this Website
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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

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

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

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

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

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

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

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

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

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

CakePHP Logging CakePHP Logging Sep 10, 2024 pm 05:26 PM

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

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

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 Quick Guide CakePHP Quick Guide Sep 10, 2024 pm 05:27 PM

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.

See all articles