How to determine whether it is in a two-dimensional array in php

PHPz
Release: 2023-04-26 14:51:22
Original
433 people have browsed it

PHP Is it in a two-dimensional array?

PHP is a server-side scripting language widely used in web development. The PHP language is flexible and powerful and suitable for various web application development. Numerous open source frameworks and libraries make web development an easy task. Among them, two-dimensional array is one of the commonly used data structures in PHP and is used in various business scenarios. In this article, we will learn in depth how PHP implements various operations on two-dimensional arrays.

What is a two-dimensional array?

Two-dimensional arrays, also known as multi-dimensional arrays, are arrays composed of one-dimensional arrays. Their elements can be accessed through two subscripts, the first is used to index the given one-dimensional array, and the second is used to index the position of the element in the one-dimensional array. This makes two-dimensional arrays in PHP very convenient when dealing with complex data structures.

How to create a two-dimensional array?

In PHP, we can use the array() function to create a two-dimensional array and store elements of various data types in it, such as strings, numbers, or even other arrays. The following is an example of using the array() function to create a simple two-dimensional array:

<?php
$my_array = array(
    array("John", "Doe", 28),
    array("Anna", "Smith", 32),
    array("Peter", "Jones", 45)
);
?>
Copy after login

The above code will create a two-dimensional array containing three sub-arrays, each sub-array containing three elements. We can add more subarrays in the same way as needed to form a two-dimensional array of any size and structure.

How to traverse a two-dimensional array?

In PHP, traversing a two-dimensional array usually requires the use of nested loops. The outer loop iterates over a one-dimensional array, while the inner loop iterates over the elements in a subarray. Here is a simple example showing how to iterate over the two-dimensional array created earlier:

<?php
$my_array = array(
    array("John", "Doe", 28),
    array("Anna", "Smith", 32),
    array("Peter", "Jones", 45)
);

for($i = 0; $i < count($my_array); $i++) {
    for($j = 0; $j < count($my_array[$i]); $j++) {
        echo $my_array[$i][$j] . " ";
    }
    echo "<br>";
}
?>
Copy after login

The output of the above code is as follows:

John Doe 28
Anna Smith 32
Peter Jones 45
Copy after login

How to find a specific value in a two-dimensional array?

In PHP, you can use various functions to search and filter values ​​in two-dimensional arrays. One of the most commonly used functions is array_search(), which finds the key of a given value in a two-dimensional array and returns the position of the key.

Here is a simple example showing how to use the array_search() function to find a specific value in a two-dimensional array:

<?php
$my_array = array(
    array("John", "Doe", 28),
    array("Anna", "Smith", 32),
    array("Peter", "Jones", 45)
);

$key = array_search("Smith", array_column($my_array, 1));

echo $key; // 输出 1
?>
Copy after login

The above code uses the array_column() function to extract the second value from the original array. column and then use the array_search() function to find "Smith" in the extracted column and return the location of that key. In this case, the return value is 1 because "Smith" is on the second row.

How to add new elements to a two-dimensional array?

In PHP, you can use various functions to add and modify elements in a two-dimensional array. The most commonly used functions are array_push() and array_pop(), which are used to add elements to or remove elements from the end of a two-dimensional array respectively.

The following is a simple example showing how to use the array_push() function to add elements to a two-dimensional array:

<?php
$my_array = array(
    array("John", "Doe", 28),
    array("Anna", "Smith", 32),
    array("Peter", "Jones", 45)
);

array_push($my_array, array("Jane", "Doe", 22));

print_r($my_array);
?>
Copy after login

The above code will add a new sub-array at the end of the two-dimensional array , and output the following content:

Array
(
    [0] => Array
        (
            [0] => John
            [1] => Doe
            [2] => 28
        )

    [1] => Array
        (
            [0] => Anna
            [1] => Smith
            [2] => 32
        )

    [2] => Array
        (
            [0] => Peter
            [1] => Jones
            [2] => 45
        )

    [3] => Array
        (
            [0] => Jane
            [1] => Doe
            [2] => 22
        )
)
Copy after login

How to delete elements in a two-dimensional array?

In PHP, you can use various functions to add and modify elements in a two-dimensional array. The most commonly used functions are array_push() and array_pop(), which are used to add elements to or remove elements from the end of a two-dimensional array respectively.

The following is a simple example showing how to use the array_pop() function to delete the last element from a two-dimensional array:

<?php
$my_array = array(
    array("John", "Doe", 28),
    array("Anna", "Smith", 32),
    array("Peter", "Jones", 45)
);

array_pop($my_array);

print_r($my_array);
?>
Copy after login

The above code will delete the last sub-array in the two-dimensional array , and output the following content:

Array
(
    [0] => Array
        (
            [0] => John
            [1] => Doe
            [2] => 28
        )

    [1] => Array
        (
            [0] => Anna
            [1] => Smith
            [2] => 32
        )
)
Copy after login

How to find elements in a two-dimensional array and edit their values?

In PHP, you can use various functions to find elements in a two-dimensional array and edit their values ​​using standard array syntax. For example, to find and edit the second element in the previous example by name, you would use code similar to the following example:

<?php
$my_array = array(
    array("John", "Doe", 28),
    array("Anna", "Smith", 32),
    array("Peter", "Jones", 45)
);

foreach($my_array as &$sub_array) {
    if($sub_array[0] == "Anna") {
        $sub_array[1] = "Johnson";
    }
}

print_r($my_array);
?>
Copy after login

The above code will iterate through the entire array and when it finds "Anna" its last name Change to "Johnson". In this case, the output updates the array of array elements accordingly as shown below:

Array
(
    [0] => Array
        (
            [0] => John
            [1] => Doe
            [2] => 28
        )

    [1] => Array
        (
            [0] => Anna
            [1] => Johnson
            [2] => 32
        )

    [2] => Array
        (
            [0] => Peter
            [1] => Jones
            [2] => 45
        )
)
Copy after login

Conclusion

Two-dimensional array is a very useful data structure in PHP programming and can be used for Various business scenarios. In a two-dimensional array, you can use various functions to add, delete, and edit elements. Proficiency in the use of two-dimensional arrays is very important for PHP developers, and it is also one of the keys to improving development efficiency.

The above is the detailed content of How to determine whether it is in a two-dimensional array in php. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!