PHP Multidimensional Array Searching (Find Key by Specific Value)

PHP multidimensional array search by value

function searchForId($id, $array) {
foreach ($array as $key => $val) {
if ($val['uid'] === $id) {
return $key;
}
}
return null;
}

This will work. You should call it like this:

$id = searchForId('100', $userdb);

It is important to know that if you are using === operator compared types have to be exactly same, in this example you have to search string or just use == instead ===.

Based on angoru answer. In later versions of PHP (>= 5.5.0) you can use one-liner.

$key = array_search('100', array_column($userdb, 'uid'));

Here is documentation: http://php.net/manual/en/function.array-column.php.

PHP Multidimensional Array Searching (Find key by specific value)

Very simple:

function myfunction($products, $field, $value)
{
foreach($products as $key => $product)
{
if ( $product[$field] === $value )
return $key;
}
return false;
}

How to search value in multidimensional array by all key and get key - value of matched data php?

My guess is you are trying to find a magic builtin function, there are none.

Simply loop over the array and test the fields you are interested in to see if the value you are interested in is in there. I use stripos() so it will ignore case, so it will match vi or Vi or VI or even vI

$array = [  ['ID' => 287, 'fname' => 'Vivek', 'sname' => 'Makwana', 'serial' => 72],
['ID' => 288, 'fname' => 'Vishal', 'sname' => 'Makwana', 'serial' => 73],
['ID' => 289, 'fname' => 'Nilesh', 'sname' => 'Nilesh', 'serial' => 75]
];


function search($array, $for)
{
$results = [];

foreach ( $array as $arr) {
$a = stripos($arr['fname'], $for);
$b = stripos($arr['sname'], $for);
if ( $a !== FALSE || $b !== FALSE ) {
// save the id
$results[] = $arr['ID'];
}
}
return $results;
}


print_r(search($array, 'vi'));

RESULTS

Array
(
[0] => 287
[1] => 288
)

PHP Search multidimensional array for value & get corresponding element value

function searchMultiArray($val, $array) {
foreach ($array as $element) {
if ($element['key'] == $val) {
return $element['field'];
}
}
return null;
}

And then:

searchMultiArray(31, $myArray);

Should return "CONSTRUCTN".

How to find the value of a multidimensional array by key

You can try this :

function findByKey($findKey, $array, $result = []) {
foreach ($array as $key => $value) {
if ($key === $findKey) {
$result[] = [$key => $value];
}
if (is_array($value)) {
$result = findByKey($findKey, $value, $result);
}
}
return $result;
}

The idea is to use a recursive function :

  1. you loop through your array
  2. for each key => value, you check if the key is what you want : if yes, add it to the result array, else go next
  3. if the value is an other array, you search inside this array if you have the key you want

Now use it :

$array = [
'type' => 'vacancy',
'needs' => ['root' => 'active'],
'market' => 'shopping',
'red' => 'color',
'education' => 'learning',
'fruits' => [
'red' => 'apple',
'cool' => 'cherry'
]
];

With key type :

$result = findByKey('type', $array);

var_dump($result);

Output is :

array(1) {
[0]=>
array(1) {
["type"]=>
string(7) "vacancy"
}
}

With key red :

$result = findByKey('red', $array);

var_dump($result);

Output is :

array(2) {
[0]=>
array(1) {
["red"]=>
string(5) "color"
}
[1]=>
array(1) {
["red"]=>
string(5) "apple"
}
}

Here is a link to test it : link



Related Topics



Leave a reply



Submit