Through this tutorial, you will learn how to search in multidimensional array for value and return key. And also learn how to search in multidimensional array for key and return value.
First, let us define what a multidimensional array is. A multidimensional array is an array that contains one or more arrays. Each array within the multidimensional array is referred to as a subarray or dimension. The subarrays can also contain other subarrays, which make up a multidimensional array.
For example, consider the following multidimensional array:
$fruits = array( array('name' => 'apple', 'color' => 'red', 'quantity' => 5), array('name' => 'banana', 'color' => 'yellow', 'quantity' => 3), array('name' => 'orange', 'color' => 'orange', 'quantity' => 7), );
This multidimensional array contains three subarrays, each of which represents a fruit. Each subarray has three key-value pairs, which represent the name, color, and quantity of the fruit.
PHP Search In Multidimensional Array By key, value
- PHP Search Multidimensional Array By value and return key
- PHP Search Multidimensional Array By key and return value
- PHP search multidimensional array for multiple values
PHP Search Multidimensional Array By value and return key
Here’s an example code in PHP that demonstrates how to search a multidimensional array by value and return the key:
<?php // Sample multidimensional array $employees = array( array('id' => 1, 'name' => 'John', 'age' => 25), array('id' => 2, 'name' => 'Jane', 'age' => 30), array('id' => 3, 'name' => 'Bob', 'age' => 27), array('id' => 4, 'name' => 'Alice', 'age' => 35) ); // Function to search the array by value and return the key function search_multidimensional_array($array, $key, $value) { foreach ($array as $subarray_key => $subarray) { if (isset($subarray[$key]) && $subarray[$key] == $value) { return $subarray_key; } } return false; } // Search the array for an employee with name 'Bob' $key = search_multidimensional_array($employees, 'name', 'Bob'); // Output the result if ($key !== false) { echo "Employee with name 'Bob' found at key: " . $key; } else { echo "Employee with name 'Bob' not found"; } ?>
PHP Search Multidimensional Array By key and return value
here’s an example of how to search a multidimensional array in PHP by a specific key and return its value:
Suppose we have an array called $users, which contains multiple arrays representing individual users, and each user array has keys such as “id”, “name”, and “email”. We want to search this array for a specific user by their ID and return their name.
$users = array( array( "id" => 1, "name" => "John Smith", "email" => "[email protected]" ), array( "id" => 2, "name" => "Jane Doe", "email" => "[email protected]" ), array( "id" => 3, "name" => "Bob Johnson", "email" => "[email protected]" ) ); $search_id = 2; // The ID of the user we want to find foreach ($users as $user) { if ($user["id"] == $search_id) { $found_name = $user["name"]; break; } } if (isset($found_name)) { echo "The name of user ID $search_id is $found_name."; } else { echo "User ID $search_id not found."; }
PHP search multidimensional array for multiple values
Here is an example of how to search a multidimensional array in PHP for multiple values:
Suppose you have a multidimensional array that contains information about various products, and you want to search for products that have both a certain category and a certain price range. The array might look like this:
$products = array( array("name" => "Product 1", "category" => "books", "price" => 10.99), array("name" => "Product 2", "category" => "books", "price" => 15.99), array("name" => "Product 3", "category" => "electronics", "price" => 29.99), array("name" => "Product 4", "category" => "electronics", "price" => 49.99), array("name" => "Product 5", "category" => "clothing", "price" => 19.99) );
To search this array for products that have both the category “books” and a price between $10 and $20, you can use the array_filter()
function in combination with an anonymous function. The anonymous function will take each element of the array as an argument, and return true
if the element matches the criteria, or false
otherwise.
Here’s the code:
// Define the search criteria
$category = "books";
$min_price = 10;
$max_price = 20;
// Define the search function
$search_function = function($product) use ($category, $min_price, $max_price) {
return ($product["category"] == $category && $product["price"] >= $min_price && $product["price"] <= $max_price);
};
// Perform the search
$results = array_filter($products, $search_function);
// Output the results
foreach ($results as $result) {
echo $result["name"] . " is in the category " . $result["category"] . " and costs $" . $result["price"] . "<br>";
}
Conclusion
The fastest way to search a multidimensional array. In this tutorial, you have learned how to search in a multidimensional array by key, value, and multiple values.
Recommended PHP Tutorials
- PHP Array: Indexed,Associative, Multidimensional
- To Remove Elements or Values from Array PHP
- PHP remove duplicates from multidimensional array
- Remove Duplicate Elements or Values from Array PHP
- How to Convert String to Array in PHP
- Array Push and POP in PHP | PHP Tutorial
- PHP Array to String Conversion – PHP Implode