Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to Search by key=value in a Multidimensional Array in PHP
In PHP, searching for a specific keyvalue pair in a multidimensional array is a common requirement when working with complex data structures. There are several effective approaches to accomplish this task.
Here are three commonly used approaches ?
Using a foreach loop
Using array_filter() and array_column()
Using array_search() with array_column()
Using a foreach loop
The foreach loop provides a straightforward way to iterate through each subarray and check for the desired keyvalue pair ?
<?php
$users = [
['id' => 1, 'name' => 'John', 'city' => 'New York'],
['id' => 2, 'name' => 'Jane', 'city' => 'London'],
['id' => 3, 'name' => 'Bob', 'city' => 'New York']
];
$searchKey = 'city';
$searchValue = 'New York';
$foundElement = null;
foreach ($users as $user) {
if (isset($user[$searchKey]) && $user[$searchKey] === $searchValue) {
$foundElement = $user;
break;
}
}
if ($foundElement !== null) {
echo "Found: " . $foundElement['name'] . " from " . $foundElement['city'];
} else {
echo "No match found";
}
?>
Found: John from New York
Using array_filter() and array_column()
The array_filter() function allows you to filter the array based on a callback function, returning all matching elements ?
<?php
$products = [
['id' => 1, 'category' => 'electronics', 'price' => 100],
['id' => 2, 'category' => 'books', 'price' => 25],
['id' => 3, 'category' => 'electronics', 'price' => 200]
];
$searchKey = 'category';
$searchValue = 'electronics';
$result = array_filter($products, function ($product) use ($searchKey, $searchValue) {
return isset($product[$searchKey]) && $product[$searchKey] === $searchValue;
});
if (!empty($result)) {
echo "Found " . count($result) . " electronic products:
";
foreach ($result as $product) {
echo "ID: " . $product['id'] . ", Price: $" . $product['price'] . "
";
}
} else {
echo "No matches found";
}
?>
Found 2 electronic products: ID: 1, Price: $100 ID: 3, Price: $200
Using array_search() with array_column()
This approach extracts a column of values and searches for the first match using array_search() ?
<?php
$employees = [
['id' => 101, 'name' => 'Alice', 'department' => 'HR'],
['id' => 102, 'name' => 'Bob', 'department' => 'IT'],
['id' => 103, 'name' => 'Carol', 'department' => 'Finance']
];
$searchKey = 'department';
$searchValue = 'IT';
$key = array_search($searchValue, array_column($employees, $searchKey));
if ($key !== false) {
echo "Found employee: " . $employees[$key]['name'] . " in " . $employees[$key]['department'];
} else {
echo "No employee found in that department";
}
?>
Found employee: Bob in IT
Comparison
| Method | Returns | Multiple Results | Best For |
|---|---|---|---|
| foreach loop | First match | No (with break) | Simple searches, custom logic |
| array_filter() | All matches | Yes | Finding multiple results |
| array_search() + array_column() | First match index | No | Quick single result lookup |
Conclusion
Each approach has its advantages depending on your needs. Use foreach for custom logic, array_filter() for multiple matches, and array_search() with array_column() for quick singleresult searches.
