Home  >  Article  >  Backend Development  >  How to express multidimensional arrays in php

How to express multidimensional arrays in php

PHPz
PHPzOriginal
2023-04-25 09:05:40728browse

In the development process of PHP, array is an important data structure, and multi-dimensional array is one of the commonly used types. Multidimensional arrays are also called two-dimensional, three-dimensional or even higher-dimensional arrays. Their basic definitions are the same as one-dimensional arrays, but there are certain differences in the storage structure of the data.

The basic expression of multi-dimensional arrays is to use one array as an element in another array, so that a multi-dimensional data structure can be realized. For example, we can define a two-dimensional array:

$matrix = array(
    array(1, 2, 3),
    array(4, 5, 6),
    array(7, 8, 9)
);

This two-dimensional array is similar to a matrix. It contains elements in 3 rows and 3 columns. Their values ​​can be accessed through subscripts, such as $matrix[0 ][0] represents element 1 in the first row and first column, $matrix1 represents element 6 in the second row and third column.

In addition, we can also use multi-dimensional arrays as attributes of an object, which can better organize the data structure. For example:

class Person {
    public $name;
    public $age;
    public $contact = array();
}

Here we define a Person class, where $name and $age are ordinary attributes, and $contact is an associative array used to store contact information. We can add data to $contact using the following method:

$person1 = new Person();
$person1->name = "Tom";
$person1->age = 25;
$person1->contact = array(
    "email" => "tom@gmail.com",
    "phone" => "123456789"
);

In this example, we create a Person object named $person1, set the name and age to Tom and 25 years old, and then set the two Contact information is added to the $contact array. When we need to access certain contact information, we can use subscripts to access it. For example, $person1->contact["email"] represents Tom's email address.

In addition, we can also use multi-dimensional arrays as parameters of functions, which can easily pass some complex data structures. For example:

function search($matrix, $value) {
    foreach ($matrix as $row) {
        foreach ($row as $item) {
            if ($item == $value) {
                return true;
            }
        }
    }
    return false;
}

$matrix = array(
    array(1, 2, 3),
    array(4, 5, 6),
    array(7, 8, 9)
);
$value = 6;

var_dump(search($matrix, $value));

In this example, we define a function named search, whose first parameter is the $matrix array, and $value represents the element that needs to be found in $matrix. Two foreach loops are used in the function to traverse the elements in $matrix. If $value is found, true is returned, otherwise false is returned. Finally we call this function and output the result.

Through the above examples, we can see that the expression of multi-dimensional arrays can be two-dimensional, three-dimensional or even higher-dimensional arrays, and they can also be combined together, such as using two-dimensional arrays as objects. properties, or pass multidimensional arrays as arguments to functions. Mastering the expression of multi-dimensional arrays can help us better handle complex data structures in PHP and improve development efficiency.

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