Home  >  Article  >  Backend Development  >  How to replace array elements with a value in php

How to replace array elements with a value in php

青灯夜游
青灯夜游Original
2022-09-20 17:39:592021browse

3 methods: 1. Use the array_splice() function to replace 1 element starting from the specified position, the syntax is "array_splice(array, starting position, 1, replacement value)"; 2. Use array_replace() The function will replace the value of the first array with the value of the following array. The syntax is "array_replace(array, replace array)". You only need to replace a value contained in the array; 3. Reassign the value of the element with the specified subscript. The syntax is " $array name[subscript]="replacement value"".

How to replace array elements with a value in php

The operating environment of this tutorial: Windows 7 system, PHP version 8.1, DELL G3 computer

php uses a value 3 ways to replace array elements

Method 1: Using the array_splice() function

array_splice() is a powerful function that can be implemented Delete, insert, and replace element operations.

array_splice($array,$start,$length,$replacement)

Parameters:

  • arr represents an array.
  • start indicates the position (subscript) where deletion begins:
    • 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 be deleted:
    • If length is a positive number, it means length elements are deleted;
    • If length is a negative number, all elements starting from start and counting down to length from the end of the array will be deleted;
    • If it is omitted, all elements starting from start and ending at 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 a replacement operation is performed, the length value and the number of replacements need to be consistent.

Note that using replacement to replace array elements will not retain the original key names.

<?php
header(&#39;content-type:text/html;charset=utf-8&#39;);   
$arr=array(1,2,3,4,5);
var_dump($arr);
array_splice($arr,1,1,"H");
var_dump($arr);
?>

How to replace array elements with a value in php

Method 2: Use array_replace() function

array_replace() function uses the value of the following array to replace the first array value.

array_replace(数组,替换数组)

You only need to replace one value contained in the array, and the first element of the array will be replaced

<?php
$a1=array(1,2,3,4,5);
var_dump($a1);
$a2=array("blue");
var_dump(array_replace($a1,$a2));
?>

How to replace array elements with a value in php

Method 3 : Reassign the value to the element with the specified subscript

<?php
$arr=array(1,2,3,4,5);
var_dump($arr);
$arr[3]="hello";
var_dump($arr);
?>

How to replace array elements with a value in php

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to replace array elements with a value in php. For more information, please follow other related articles on the PHP Chinese website!

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