Home > Backend Development > PHP Problem > How to query the position of elements in a two-dimensional array in php

How to query the position of elements in a two-dimensional array in php

PHPz
Release: 2023-04-19 14:01:14
Original
952 people have browsed it

In PHP, sometimes we need to find the position of a specific element in a two-dimensional array. The process can be very simple, but it can also become very tricky if you don't know the correct method. This article will introduce several methods of querying the position of elements in a two-dimensional array in PHP and help you solve this problem.

Querying the position of individual elements

If you want to query the position of a specific element in a two-dimensional array, you can use a two-level for loop statement. First loop through each sub-array, and then find the target element in each sub-array. If the target element is found, its position can be returned.

The following is a sample code that uses a for loop statement to query the position of elements in a two-dimensional array:

function find_element($arr, $target) {
    for ($i = 0; $i < count($arr); $i++) {
        for ($j = 0; $j < count($arr[$i]); $j++) {
            if ($arr[$i][$j] == $target) {
                return array($i, $j);
            }
        }
    }
}
Copy after login

In this function, the first parameter $arr represents the two-dimensional array we are looking for. , the second parameter $target represents the target element we want to find. Use two for loop statements to traverse the array, and if the target element is found, its position is returned.

For example, suppose we have a two-dimensional array as follows:

$arr = array(
    array(1, 2, 3),
    array(4, 5, 6),
    array(7, 8, 9)
);
Copy after login
Copy after login
Copy after login

We can use the following code to find the position of the number 5:

$target = 5;
$position = find_element($arr, $target);
echo "The position of $target is ({$position[0]}, {$position[1]})";
Copy after login
Copy after login
Copy after login

This will output: The position of 5 is (1, 1).

Use the array_search() function to query the position

In addition to manually writing a for loop, we can also use PHP's array_search() function to query the position of a specific element in a two-dimensional array. The array_search() function can only find elements in a one-dimensional array, but cannot directly search in a two-dimensional array. Therefore, we need to "flatten" the two-dimensional array into a one-dimensional array and then find the position of the target element. We can then use some mathematical operations to convert the positions of the one-dimensional array into the positions of the two-dimensional array.

The following is an example code that "flattens" a two-dimensional array into a one-dimensional array and uses the array_search() function to find the target element:

function find_element($arr, $target) {
    $flatten = array();
    foreach ($arr as $subarr) {
        $flatten = array_merge($flatten, $subarr);
    }
    $key = array_search($target, $flatten);
    if ($key !== false) {
        $col = $key % count($arr[0]);
        $row = floor($key / count($arr[0]));
        return array($row, $col);
    }
}
Copy after login

In this function, we first convert the two-dimensional array into a one-dimensional array. The array is "flattened" into a one-dimensional array $flatten. We then use the array_search() function to find the location of the target element in $flatten. If the target element is found, we can use some mathematical operations to convert the one-dimensional position into a two-dimensional position.

For example, suppose we have a two-dimensional array as follows:

$arr = array(
    array(1, 2, 3),
    array(4, 5, 6),
    array(7, 8, 9)
);
Copy after login
Copy after login
Copy after login

We can use the following code to find the position of the number 5:

$target = 5;
$position = find_element($arr, $target);
echo "The position of $target is ({$position[0]}, {$position[1]})";
Copy after login
Copy after login
Copy after login

This will output: The position of 5 is (1, 1).

Use the array_filter() function to query the position

Similar to the array_search() function, we can also use the array_filter() function and some mathematical operations to query the position of a specific element in a two-dimensional array. Using the array_filter() function avoids the problem of manually looping and "flattening" arrays.

The following is a sample code that uses the array_filter() function and mathematical operations to query the position of a specific element in a two-dimensional array:

function find_element($arr, $target) {
    $result = array_filter($arr, function ($subarr) use ($target) {
        return in_array($target, $subarr);
    });
    if (!empty($result)) {
        $row = key($result);
        $col = array_search($target, $arr[$row]);
        return array($row, $col);
    }
}
Copy after login

In this function, we use the array_filter() function to filter the two-dimensional Array, retaining only the subarray containing the target element. Then, we can use the key() function and array_search() function to find the location of the target element.

For example, suppose we have a two-dimensional array as follows:

$arr = array(
    array(1, 2, 3),
    array(4, 5, 6),
    array(7, 8, 9)
);
Copy after login
Copy after login
Copy after login

We can use the following code to find the position of the number 5:

$target = 5;
$position = find_element($arr, $target);
echo "The position of $target is ({$position[0]}, {$position[1]})";
Copy after login
Copy after login
Copy after login

This will output: The position of 5 is (1, 1).

Summary

Querying a position in a two-dimensional array in PHP can get tricky, but there are a few different methods we can use to solve this problem. Whether you write a for loop by hand, use the array_search() function, or use the array_filter() function, some math is required to convert a one-dimensional position to a two-dimensional position. Hopefully this article helps you better understand how to query a position in a 2D array in PHP.

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