How to change the value of the last element of an array in php

Change method: 1. Directly reassign the last element of the array, syntax “$arr[sizeof($arr)-1]=new value;”; 2. Use the array_splice() function to replace the value of the last array element, syntax ” array_splice($arr,-1,1,”new value”)”.
The operating environment of this tutorial: Windows7 system, PHP7.1 version, DELL G3 computer
php change last value of array
$arr[array length-1]Method 1: Directly reassign the last element of the array ( )
$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
The array_splice() function removes the selected element from the array and replaces it with the new element. The function will also return an array with the elements removed.
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);
?>