Home  >  Article  >  Backend Development  >  How to replace elements in an array in PHP

How to replace elements in an array in PHP

青灯夜游
青灯夜游Original
2022-05-07 16:45:384269browse

Replacement method: 1. Use "array_replace (array, replace array)" to replace the elements of the first array with the elements of the subsequent array. 2. Use "array_splice(array, starting position, number, replacement value)" to replace the specified number of elements starting from the specified position. If multiple values ​​are replaced, the replacement value can be an array.

How to replace elements in an array in PHP

The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer

PHP replacement array Two methods for elements:

1. Use array_replace() function

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

array_replace(数组,替换数组)

Note: There can be multiple replacement arrays, separated by , .

<?php
$a1=array(1,2,3,4,5);
$a2=array("blue","yellow");
$a3=array("a","b","c","d","e","f");
var_dump(array_replace($a1,$a2));
var_dump(array_replace($a1,$a3));
?>

How to replace elements in an array in PHP

If you specify multiple arrays to be replaced, the values ​​of the subsequent arrays will overwrite the values ​​of the previous arrays.

<?php
$a1=array(1,2,3,4,5);
$a2=array("blue","yellow");
$a3=array("a","b","c","d","e","f");
var_dump(array_replace($a1,$a3,$a2));
var_dump(array_replace($a1,$a2,$a3));
?>

How to replace elements in an array in PHP

2. Use array_splice() function

array_splice() is a powerful function that can realize deletion, insertion, Replace element operation.

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 it from the back to the 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, it means deleting length elements;
    •           If length is a negative number, then all elements starting from start and counting down to length at the end of the array will be deleted;
    •                    If omitted, all elements starting at 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);
array_splice($arr,1,3,array("a","b","c"));
var_dump($arr);
?>

How to replace elements in an array in PHP

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to replace elements in an array 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