How to create an array in php and save the array

王林
Release: 2023-05-19 21:34:06
Original
487 people have browsed it

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);
Copy after login

This syntax requires specifying the value of each element when creating the array, for example:

$fruits = array("apple", "banana", "orange");
Copy after login

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");
Copy after login

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 [] symbol

In PHP 5.4 and above, you can also use the [] symbol to create an array. The syntax is as follows:

$array = [value1, value2, ..., valueN];
Copy after login

The following is an Example of creating an array using the [] notation:

$colors = ["red", "green", "blue"];
Copy after login

This will create a $colors array containing three elements, namely "red", "green" and "blue".

2. Save an array to another array

In 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;
Copy after login

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
        )

)
Copy after login

2. Use the array_merge() function

If 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);
Copy after login

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);
Copy after login

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
)
Copy after login

3. Summary

Array 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!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!