PHP multidimensional array search by value
function find_customer_mobile($customers, $mobile) {
foreach($customers as $index => $cust) {
if($cust['mobile'] == $mobile) return $index;
}
return FALSE;
}
4.13
8
/**
* PHP Search an Array for multiple key / value pairs
*/
function multi_array_search($array, $search) {
// Create the result array
$result = array();
// Iterate over each array element
foreach ($array as $key => $value){
// Iterate over each search condition
foreach ($search as $k => $v){
// If the array element does not meet the search condition then continue to the next element
if (!isset($value[$k]) || $value[$k] != $v){
continue 2;
}
}
// Add the array element's key to the result array
$result[] = $key;
}
// Return the result array
return $result;
}
// Output the result
print_r(multi_array_search($list_of_phones, array()));
// Array ( [0] => 0 [1] => 1 )
// Output the result
print_r(multi_array_search($list_of_phones, array('Manufacturer' => 'Apple')));
// Array ( [0] => 0 )
// Output the result
print_r(multi_array_search($list_of_phones, array('Manufacturer' => 'Apple', 'Model' => 'iPhone 6')));
// Array ( )
Thank you!
8
0
3
1
$key = array_search('100', array_column($userdb, 'uid'));
Thank you!
1
0
Are there any code examples left?
New code examples in category PHP
-
PHP 2022-03-27 22:40:03 index:of curiosa 2019 mp4
-
PHP 2022-03-27 22:35:13 Dijkstra’s algorithm php
-
PHP 2022-03-27 22:35:12 get_declared_classes
-
PHP 2022-03-27 22:30:23 php move index of a value to first position in array
-
PHP 2022-03-27 22:25:01 wordpress get_date
-
PHP 2022-03-27 21:30:38 php shorten string with dots
-
PHP 2022-03-27 21:10:46 php header 500
-
PHP 2022-03-27 21:05:06 laravel schedule run
-
PHP 2022-03-27 20:25:12 Get page title, excerpt or content by id
-
PHP 2022-03-27 20:00:27 php convert date from dd/mm/yyyy to yyyy-mm-dd