Home > Backend Development > PHP Tutorial > How to find multiple values ​​corresponding to a key name in an array in PHP

How to find multiple values ​​corresponding to a key name in an array in PHP

PHPz
Release: 2023-07-08 06:40:01
Original
1591 people have browsed it

How to find multiple values ​​corresponding to key names in an array in PHP

In PHP, an array is a common data structure used to store a set of related data. Sometimes we need to find the corresponding value based on the key name of the array, and there may be multiple values ​​corresponding to the same key name. This article will introduce how to find multiple values ​​corresponding to key names in an array in PHP, and illustrate it with code examples.

First, we need to define an array containing multiple values. Taking student information as an example, we can define a student array, and each student's name (key name) can correspond to multiple hobbies (values).

Code example:

$students = array(
    'Alice' => array('Reading', 'Painting'),
    'Bob' => array('Singing', 'Dancing'),
    'Charlie' => array('Swimming', 'Running'),
    'Alice' => array('Cooking', 'Gardening')
);
Copy after login

The above code creates a student array, where the key is the student's name, and the corresponding value is an array containing the student's hobbies.

Next, we can use a foreach loop to traverse the array and determine whether each key name matches the key name we are looking for. If there is a match, the corresponding value is placed into a new array.

Code example:

$searchKey = 'Alice';
$result = array();

foreach ($students as $key => $value) {
    if ($key == $searchKey) {
        $result = array_merge($result, $value);
    }
}
Copy after login

In the above code, we define a $searchKey variable to store the key name to be found. Then, we create an empty array $result to store the multiple values ​​found. Use a foreach loop to traverse the $students array. If $key is equal to $searchKey, merge the corresponding $value array into the $result array.

Finally, we can view the search results by printing the $result array.

Code example:

print_r($result);
Copy after login

In the above code, we use the print_r function to print the $result array. The print_r function is used to output easy-to-understand information about variables.

Run the above code, the output result will be:

Array
(
    [0] => Reading
    [1] => Painting
    [2] => Cooking
    [3] => Gardening
)
Copy after login

As you can see, we successfully found multiple values ​​corresponding to the key name 'Alice' in the array.

It should be noted that the above method can only find multiple values ​​corresponding to the first matching key name. If we want to find multiple values ​​corresponding to all matching key names, we can use a loop to store all matching results.

To sum up, this article introduces how to find multiple values ​​corresponding to key names in an array in PHP, and gives corresponding code examples. By using foreach loop and conditional judgment, we can easily implement this function.

The above is the detailed content of How to find multiple values ​​corresponding to a key name in an array in PHP. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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