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

How to write array in php

PHPz
PHPzOriginal
2023-04-20 15:01:23555browse

PHP is a very popular programming language, and arrays are one of the very important data types in PHP. Arrays allow developers to store and manipulate data more efficiently, so it is very necessary to learn how to use arrays to master PHP programming. So this article will introduce to you how to write arrays in PHP.

1. What is an array

Before introducing how to write an array, let’s first understand what an array is. An array is a container that can store multiple data items, and these data items can be of different types. In PHP, an array can be defined as an ordered, associative collection, in which each element is mapped between a key name and a key value. Simply put, a PHP array is a data structure consisting of one or more key and value pairs.

In PHP, an array can be an indexed array, an associative array or a multidimensional array. In an indexed array, each array item has a numeric key by which the corresponding value can be found. In an associative array, each array item has a key name through which the corresponding value can be found. A multidimensional array is an array that contains other arrays, which can be associative arrays or indexed arrays.

For example, the following is an example of an associative array:

$person = array("name" => "Tom", "age" => 20, "gender" => "male");

In the above array, the key names are "name", "age" and "gender", and the corresponding key values ​​are "Tom", "20" and "male".

2. Write an array in PHP

In PHP, we can create an array in the following ways:

1. Use the array() function to create an array Array

The most basic method is to use the array() function, which can create an empty array or an array containing multiple elements. For example:

$array = array(); // 创建空数组

$array = array(1,2,3); // 创建索引数组

$array = array(
            "name" => "Tom",
            "age" => 22,
            "gender" => "male"
         ); // 创建关联数组

2. Use [] to create an array

For PHP5.4 and above, you can use the [] symbol to create an array. For example:

$array = []; // 创建空数组

$array = [1,2,3]; // 创建索引数组

$array = [
            "name" => "Tom",
            "age" => 22,
            "gender" => "male"
         ]; // 创建关联数组

3. Create arrays through loops

We can also use loop statements to create arrays. For example:

$array = array(); // 创建空数组
for ($i=0; $i<10; $i++) {
    $array[] = $i; // 在数组尾部插入元素
}

The above code will produce an index array containing 0 to 9.

4. Use the list() function to create an array

We can use the list() function to assign the values ​​​​in an array to a set of variables, or we can use it to create an array. For example:

list($a, $b, $c, $d) = array(1, 2, 3, 4); // 将数组中的值赋给变量

$array = array();
list($array[]) = "first";// 将字符串"first"插入到数组尾部
list($array[]) = "second";// 将字符串"second"插入到数组尾部
list($array[]) = "third";// 将字符串"third"插入到数组尾部

The above code will produce an index array containing three strings.

3. Using arrays

After creating the array, we can access the elements in the array through key names or key values. For example:

$array = array(
            "name" => "Tom",
            "age" => 22,
            "gender" => "male"
         );

echo $array["name"];// 输出"Tom"

We can also use a foreach loop to iterate through all elements in the array. For example:

foreach ($array as $key => $value) {
    echo "Key: " . $key . " Value: " . $value . "<br />";
}

This code will output the key names and key values ​​of all elements in the array.

Finally, we can also dynamically add, delete and modify elements in the array. For example:

$array = array(
            "name" => "Tom",
            "age" => 22,
            "gender" => "male"
         );

$array["hometown"] = "Shanghai";// 添加元素

unset($array["age"]);// 删除元素

$array["gender"] = "female";// 修改元素

The above is a detailed introduction on how to write arrays in PHP. I hope it can help you better use arrays in PHP development.

The above is the detailed content of How to write 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