array("value1","/> array("value1",">

Home >Backend Development >PHP Problem >How to write associative two-dimensional array in php

How to write associative two-dimensional array in php

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2023-05-11 11:43:07489browse

Associative two-dimensional array is one of the most commonly used data structures in PHP programming. In some cases, it can greatly simplify code writing and improve program efficiency. This article will introduce in detail the definition, initialization, access, traversal and common operations of associative two-dimensional arrays in PHP.

1. Definition and initialization

Associated two-dimensional arrays are usually defined and initialized using the array() function. The syntax format is as follows:

$array = array(
    "key1" => array("value1", "value2", "value3"),
    "key2" => array("value4", "value5", "value6"),
    "key3" => array("value7", "value8", "value9"),
);

Among them, $array is the key The defined associated two-dimensional array name, key and value are the key and value of the array respectively, which can be any string or integer. Each element of the array contains an array, which is a two-dimensional array.

You can also use square bracket notation to define an associated two-dimensional array, as follows:

$array = [
    "key1" => ["value1", "value2", "value3"],
    "key2" => ["value4", "value5", "value6"],
    "key3" => ["value7", "value8", "value9"],
];

2. Access array elements

You can use square bracket notation to access array elements , as shown below:

echo $array["key1"][0]; //输出value1
echo $array["key2"][1]; //输出value5

You can also loop through the entire array to obtain all elements, as shown below:

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

Among them, the first loop traverses to obtain the key name of the associated array and the corresponding One-dimensional array, the second loop traverses to obtain all elements of the one-dimensional array.

3. Traversing arrays

The traversal of associated two-dimensional arrays is slightly different from the general array traversal, and a double foreach loop needs to be used, as shown below:

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

Among them, The first loop traverses to obtain the key names of the associative array and the corresponding one-dimensional array, and the second loop traverses to obtain all elements of the one-dimensional array.

4. Common operations

  1. Add elements

You can use square bracket notation to add new elements, as shown below:

$array["key4"] = array("value10", "value11", "value12");
  1. Delete elements

You can use the unset function to delete an element of the array, as shown below:

unset($array["key2"]);
  1. Modify elements

You can modify the elements in the array through direct assignment, as shown below:

$array["key1"][0] = "new value1";
  1. Array sorting

Associated two-dimensional arrays also support the sort() function Sort as follows:

sort($array);

5. Summary

Associative two-dimensional array is a commonly used data structure in PHP, which can greatly simplify the writing of code, improve the efficiency of the program, and master its definition. Knowledge of initialization, access, traversal and common operations is very important for PHP programming.

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