"John", "age" => 20, "gender" => "male"), array("n"/> "John", "age" => 20, "gender" => "male"), array("n">

Home  >  Article  >  Backend Development  >  How to query a specified value in a PHP array

How to query a specified value in a PHP array

PHPz
PHPzOriginal
2023-04-18 09:47:17644browse

When you have a PHP array with a large amount of data, have you ever encountered a situation where you want to find a specific value? In this article, we will introduce how to query a specified value in a PHP array.

First, let's look at an example array to better understand the problem:

$data = array(
    array("name" => "John", "age" => 20, "gender" => "male"),
    array("name" => "Lisa", "age" => 25, "gender" => "female"),
    array("name" => "Jack", "age" => 30, "gender" => "male"),
    array("name" => "Emily", "age" => 35, "gender" => "female"),
);

This is an array of four people, each with a name, age and gender . We will use this array to demonstrate how to query a specified value in an array in PHP.

Method 1: Use foreach loop

The first method is to use PHP's foreach loop to traverse the array, and then use an if statement to check whether each value is equal to the value we are looking for. This method is very simple and straightforward and works well for small arrays, but becomes less efficient when the array is larger.

The following is sample code using a foreach loop:

$search = "Lisa";
foreach($data as $key => $value) {
    if($value['name'] == $search) {
        echo "Name: ".$value['name'].", Age: ".$value['age'].", Gender: ".$value['gender']."
";     } }

Output results:

Name: Lisa, Age: 25, Gender: female

We use the search variable to store the name we are looking for, and the "foreach" loop iterates through each value , and checks if the value is equal to the name stored in the "search" variable. If so, print the relevant information.

Method 2: Use PHP built-in function array_filter

The second method uses PHP's array_filter function, which will return a new array containing values ​​that meet our check conditions.

The following is a sample code using the array_filter function:

$search = "male";
$newArray = array_filter($data, function ($value) use ($search) {
    return $value['gender'] == $search;
});
foreach($newArray as $key => $value) {
    echo "Name: ".$value['name'].", Age: ".$value['age'].", Gender: ".$value['gender']."
"; }

Output results:

Name: John, Age: 20, Gender: male
Name: Jack, Age: 30, Gender: male

We use the search variable to store the gender we are looking for, the "array_filter" function will check each Whether the gender of the value is equal to the gender stored in the "search" variable, and if so, appended to a new array. Finally, we use a foreach loop to output information about each value in the new array.

Method 3: Use PHP built-in function array_search

The third method is to use PHP's array_search function, which will return a key whose value appears for the first time in the array, and only applies in a one-dimensional array.

The following is a sample code using the array_search function:

$search = "Lisa";
$key = array_search($search, array_column($data, 'name'));
echo "Name: ".$data[$key]['name'].", Age: ".$data[$key]['age'].", Gender: ".$data[$key]['gender']."
";

Output results:

Name: Lisa, Age: 25, Gender: female

We use the search variable to store the name we want to find, and then use the "array_column" function to extract the name from the multidimensional Get the specified "name" column in the array. Next, use the "array_search" function to search for the first occurrence of the key in the array and store it in the key variable. Finally, use the key variable to output relevant information that matches the name we are looking for.

Summary

There are many ways to query a specified value in PHP. Which method to choose depends on the size of your array and your code implementation needs. Whichever method you use will ensure you have fast access to data when working with large amounts of data, thereby improving your website performance and making your development more efficient.

The above is the detailed content of How to query a specified value in a PHP array. 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