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);
$fruits = array("apple", "banana", "orange");$person = array("name" => "Tom", "age" => 20, "gender" => "male");$array = [value1, value2, ..., valueN];
$colors = ["red", "green", "blue"];
$array1 = ["Apple", "Banana", "Orange"]; $array2 = ["Pear", "Grape", "Cherry"]; $array1[] = $array2;
Array
(
[0] => Apple
[1] => Banana
[2] => Orange
[3] => Array
(
[0] => Pear
[1] => Grape
[2] => Cherry
)
)$new_array = array_merge($array1, $array2);
$array1 = ["Apple", "Banana", "Orange"]; $array2 = ["Pear", "Grape", "Cherry"]; $new_array = array_merge($array1, $array2);
Array
(
[0] => Apple
[1] => Banana
[2] => Orange
[3] => Pear
[4] => Grape
[5] => Cherry
)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!
How to remove people from the blacklist on WeChat
What to do if the documents folder pops up when the computer is turned on
How to set page numbers in word
What are the problems with using php
How to make a call without showing your number
How is the performance of php8?
css3transition
What is the principle and mechanism of dubbo