Home > Article > Backend Development > How to modify static array in php
In PHP, arrays are a very common and commonly used data type. The essence of an array is a variable, and it stores a set of data of the same type. Arrays in PHP can be divided into two categories, one is a numeric index array, called an index array; the other is a string index array, called an associative array. Among them, the index of the index array is of integer type, while the index of the associative array is of string type.
It is very simple to modify a static array in PHP. You only need to access a certain position of the array through "array variable name [index]" and then modify it directly. The following is a sample code:
<?php $arr = array("apple", "banana", "orange"); $arr[1] = "grape"; print_r($arr); ?>
In the above code, we first define an index array $arr
, which contains three elements: apple, banana and orange. Next, we use $arr[1]
to access the second element in the array (since array indexing starts at 0 in PHP) and change it to grape. Finally, we used the print_r()
function to output the modified array, and the output result is:
Array ( [0] => apple [1] => grape [2] => orange )
Through this example, we can see that as long as the array variable name is used plus square brackets , and then put the index number of the array element to be modified in square brackets, you can easily modify the static array.
Of course, in actual development, if it is just to modify an array element, the above method is still troublesome, because the index number of the element needs to be found first. Therefore, we can use the PHP built-in function array_search()
to find the position of the element that needs to be modified in the array, and then modify it. The following is a sample code:
<?php $arr = array("John", "Tom", "David"); $search = array_search("Tom", $arr); if ($search !== false) { $arr[$search] = "Jerry"; print_r($arr); } ?>
In the above code, we first define an index array $arr
, which contains three elements: John, Tom and David. Next, we use the array_search()
function to find the element "Tom" in the array, and if found, use $arr[$search]
to modify the element. Finally, the modified array is output using the print_r()
function.
As you can see, the advantage of using the array_search()
function is that we do not need to find the index number of the element to be modified in the array ourselves, the function will automatically return the position. Of course, before using this function, we need to first determine whether it has found the element that needs to be modified. If it returns false, it means that the element does not exist in the array.
In PHP, modifying array elements is a common operation, and the operation is very simple. Whether you use the index number to make modifications or look up the index number first and then modify it, it is very easy to master. Therefore, it is very important for PHP developers to master the relevant knowledge of arrays. Hope this article is helpful to readers!
The above is the detailed content of How to modify static array in php. For more information, please follow other related articles on the PHP Chinese website!