Home  >  Article  >  Backend Development  >  How to get the value of two-dimensional array in php

How to get the value of two-dimensional array in php

PHPz
PHPzOriginal
2023-04-24 14:49:001361browse

In PHP, a two-dimensional array can be seen as an array nested within another array. Among them, the key of the outer array can be any type of data, and the value is an array. The key of the inner array can also be any type of data, and the value can be any type of data. In actual development, two-dimensional arrays are widely used on many occasions. Sometimes you need to get the values ​​in a two-dimensional array, then you need to use related functions.

1. Access the value of the two-dimensional array

To access the value of the two-dimensional array, you need to specify the subscript in sequence, that is, by specifying the key of the outer array and the key of the inner array. Values ​​in a two-dimensional array. The following is an example:

$arr = array(
    "first" => array(
        "name" => "Tom",
        "age" => 18,
        "gender" => "male"
    ),
    "second" => array(
        "name" => "Jerry",
        "age" => 19,
        "gender" => "female"
    )
);

echo $arr["first"]["name"];

In the above example, we define a two-dimensional array $arr, which has two elements, namely "first" and "second". Each element is an array containing different key-value pairs. We access the value corresponding to the "name" key in the "first" element by specifying the subscript. The result should be "Tom".

2. Traverse a two-dimensional array

You need to use a loop statement to traverse a two-dimensional array. Since the two-dimensional array nests another array, we need to use two loop statements to traverse the outer array and the inner array respectively. Here is an example:

$arr = array(
    "first" => array(
        "name" => "Tom",
        "age" => 18,
        "gender" => "male"
    ),
    "second" => array(
        "name" => "Jerry",
        "age" => 19,
        "gender" => "female"
    )
);

foreach ($arr as $key1 => $value1){
    echo $key1 . '<br>';
    foreach ($value1 as $key2 => $value2){
        echo $key2 . ': ' . $value2 . '<br>';
    }
    echo '<br>';
}

In the above example, we use two foreach loop statements. In the outer loop, $key1 refers to the key of the element in the outer array, and $value1 refers to the value of the element in the outer array (that is, the inner array). In the inner loop, $key2 refers to the key of the element in the inner array, and $value2 refers to the value of the element in the inner array. The final result output is as follows:

first
name: Tom
age: 18
gender: male

second
name: Jerry
age: 19
gender: female

3. Find the value of the two-dimensional array through keywords

In actual development, sometimes we need to query the two-dimensional array. We can operate on arrays through some built-in functions. Here, we will use the array_column() function to implement operations on two-dimensional arrays.

array_column(array $input, mixed $column_key [, mixed $index_key = null])

The function of this function is to take out a two-dimensional array according to the specified key and return a one-dimensional array. dimensional array. Among them, the $input parameter is the two-dimensional array that needs to be processed, the $column_key parameter is the key that needs to be taken out, and the $index_key parameter is the key that needs to be used as the key of the new array.

The following is an example:

$arr = array(
    array("name"=>"Tom", "age"=>18, "gender"=>"male"),
    array("name"=>"Jerry", "age"=>19, "gender"=>"female"),
    array("name"=>"Lucy", "age"=>17, "gender"=>"female")
);

$names = array_column($arr, 'name');
$genders = array_column($arr, 'gender');

print_r($names);
print_r($genders);

In the above example, we define a two-dimensional array $arr, where each element contains "name", "age" and "gender" "Three keys. We use the array_column() function to retrieve the values ​​corresponding to all "name" and all "gender" respectively. The final output result is as follows:

Array
(
    [0] => Tom
    [1] => Jerry
    [2] => Lucy
)

Array
(
    [0] => male
    [1] => female
    [2] => female
)

During development, operations on two-dimensional arrays need to be performed according to the actual situation. Mastering these operations will be beneficial to our use in development.

The above is the detailed content of How to get the value of 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
Previous article:How to write php arrayNext article:How to write php array