Home > Article > Backend Development > How to create an array in php and save the array
Array is a very commonly used data type in PHP. It can save multiple values and access them in the form of key-value pairs. In some development, we need to create an array and then save this array to another array. This article will introduce how to create an array in PHP and save it to other arrays.
1. Create an array
Creating an array in PHP is very simple. You can use the following two methods:
1. Use the array() function
## The #array() function can be used to create a new array. The syntax is as follows:$array = array(value1, value2, ..., valueN);This syntax requires specifying the value of each element when creating the array, for example:
$fruits = array("apple", "banana", "orange");This will Create a $fruits array containing three elements, namely "apple", "banana" and "orange". It is worth noting that when creating an array in this way, PHP will automatically set the key value of the element to an integer index, starting from 0. If you need to set a key name, you can use the following form:
$person = array("name" => "Tom", "age" => 20, "gender" => "male");This will create a $person array containing three elements, representing name, age and gender respectively. The key names here are "name", "age" and "gender". 2. Use the [] symbolIn PHP 5.4 and above, you can also use the [] symbol to create an array. The syntax is as follows:
$array = [value1, value2, ..., valueN];The following is an Example of creating an array using the [] notation:
$colors = ["red", "green", "blue"];This will create a $colors array containing three elements, namely "red", "green" and "blue". 2. Save an array to another arrayIn PHP, if you want to save an array to another array, you can use the following two methods: 1. Use the [] symbol . This method is relatively simple. You only need to add the [] symbol directly after the target array and write the array to be added in it, for example:
$array1 = ["Apple", "Banana", "Orange"]; $array2 = ["Pear", "Grape", "Cherry"]; $array1[] = $array2;This will create an $array1 array with four elements, the last element being the $array2 array. If we print the $array1 array, the result is as follows:
Array ( [0] => Apple [1] => Banana [2] => Orange [3] => Array ( [0] => Pear [1] => Grape [2] => Cherry ) )2. Use the array_merge() functionIf you want to merge two arrays into a new array, you can use the array_merge() function . The specific syntax is as follows:
$new_array = array_merge($array1, $array2);Among them, $array1 and $array2 respectively represent the two arrays to be merged, and $new_array is the new array after the merge. Here is an example:
$array1 = ["Apple", "Banana", "Orange"]; $array2 = ["Pear", "Grape", "Cherry"]; $new_array = array_merge($array1, $array2);This will create a $new_array array with six elements, all elements of $array1 and $array2. If we print the $new_array array, the result is as follows:
Array ( [0] => Apple [1] => Banana [2] => Orange [3] => Pear [4] => Grape [5] => Cherry )3. SummaryArray is a very important data type in PHP. It can be used to save values, strings and other types of data. , and assign key names for access. In development, there are also many techniques for using arrays, such as saving one array into another array. This article briefly introduces how to create an array and save it to other arrays. I hope it will be helpful to everyone's development.
The above is the detailed content of How to create an array in php and save the array. For more information, please follow other related articles on the PHP Chinese website!