Home  >  Article  >  Backend Development  >  How to write an array in php

How to write an array in php

PHPz
PHPzOriginal
2023-04-18 10:18:27623browse

In PHP, array is a very practical data structure that can be used to store a set of ordered data. In PHP, there are many ways to create and operate arrays. Next we will focus on how to write an array.

1. Use the array() function

The array() function is one of the most basic methods to create an array in PHP. You can use it to create an array directly. The specific implementation method is as follows:

$myArray = array('apple', 'banana', 'orange');

In the above code, we created an array named $myArray and wrote three elements into the array, namely 'apple', 'banana' and 'orange'.

2. Use square brackets []

In addition to using the array() function, you can also use square brackets [] to create and write arrays. The specific implementation method is as follows:

$myArray[] = 'apple';
$myArray[] = 'banana';
$myArray[] = 'orange';

In the above code, we created an array named $myArray and wrote three elements into the array by using square brackets [] continuously, respectively. are 'apple', 'banana' and 'orange'.

3. Use key => value format

In addition to the value of the array element, you can also specify a key name (key) for each array element to facilitate updating in subsequent operations. Convenient to use. The specific implementation method is as follows:

$myArray = array(
    'fruit1' => 'apple',
    'fruit2' => 'banana',
    'fruit3' => 'orange'
);

In the above code, we created an array named $myArray and wrote three elements into the array, namely 'apple', 'banana' and 'orange'. Different from the first two methods, we assign a key name to each array element in the form of key => value, which are 'fruit1', 'fruit2' and 'fruit3' respectively.

4. Multidimensional Array

In some cases, we may need to create a multidimensional array, that is, an array contains another or multiple arrays. The specific implementation method is as follows:

$myArray = array(
    'fruit1' => array(
        'name' => 'apple',
        'color' => 'red'
    ),
    'fruit2' => array(
        'name' => 'banana',
        'color' => 'yellow'
    ),
    'fruit3' => array(
        'name' => 'orange',
        'color' => 'orange'
    )
);

In the above code, we created a multi-dimensional array named $myArray and wrote three elements into it, namely 'apple', 'banana' and 'orange'. Each element is a subarray containing the two keys 'name' and 'color'.

Summary:

In PHP, creating and writing arrays is a very basic operation and one of the keys to writing efficient code. Whether you use the array() function, square brackets [], or the key => value form, you must pay attention to details such as the type of the array and the key names of the elements. By mastering these techniques, we can create and manipulate arrays more conveniently, quickly, and accurately.

The above is the detailed content of How to write an array in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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