PHP Multidimensional Array Search by 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 Search Multidimensional Array for Value

Nice choice of array_column()! Just extract an array with Country as the key and Out_Count as the value:

$los = 'Belgium';
$result = array_column($outs, 'Out_Count', 'Country')[$los];

To do it your way:

$los = 'Belgium';
$key = array_search($los, array_column($outs, 'Country'));
$result = $outs[$key]['Out_Count'];

Or:

$result = $outs[array_search($los, array_column($outs, 'Country'))]['Out_Count'];

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".

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;
}

PHP multidimensional array search by value using another value

Try Something like this , here i am storing uid to an array and checking is it already available in the array with in_array function

<?php
$userdb = array(
array(
'uid' => '100',
'name' => 'Sandra Shush',
'pic_square' => 'urlof100'
),
array(
'uid' => '5465',
'name' => 'Stefanie Mcmohn',
'pic_square' => 'urlof100'
),
array(
'uid' => '40489',
'name' => 'Michael',
'pic_square' => 'urlof40489'
),
array(
'uid' => '40489',
'name' => 'Jane Doe',
'pic_square' => 'urlof40489'
));
$newUserDb = array();

$arrayUserId=array();
foreach ($userdb as $user) {
$newUserDbSingle=array();
$nameArray=array();
if (in_array($user['uid'], $arrayUserId))
{
$key = array_search ($user['uid'], $arrayUserId);
$nameArray=$newUserDb[$key]['name'];
array_push($newUserDb[$key]['name'], $user['name']);

}
else
{
unset($user['pic_square']);
$arrayUserId[]=$user['uid'];
$newUserDbSingle=$user;
$newUserDbSingle['name']=array($user['name']);
$newUserDb[]=$newUserDbSingle;
}
}
print_r($newUserDb);
?>

PHP - get main array key search by value into two-level multidimensional array

You may try the following for your multi-dimensional array:

function myCustomSearch($data,$searchVal){
foreach($data as $index => $subArray){
foreach($subArray as $key=>$value){
if($value === $searchVal)return $index;
}
}
return null; //return null if not found
}

eg use assuming your current array shared in question is stored in $data

$resultIndex = myCustomSearch($data,'test3');

Let me know if this works for you.

array_search for multidimensional array with two attributes

It's probably simplest just to use a foreach over the values e.g. this function will return a similar value to array_search:

function find_user($userdb, $attributes) {
foreach ($userdb as $key => $user) {
if (empty(array_diff($attributes, $user))) return $key;
}
return false;
}

echo find_user($userdb, array('name' => 'Stefanie Mcmohn', 'uid' => 5465));

Output

1

Demo on 3v4l.org

How to search a multi-dimensional array by multiple values at once in PHP?

You can build your search query as an array and compare the intersection of each item with it.

$search = ['score' => '50', 'name' => 'Bob'];

foreach($data_info_array as $k => $v) {
if ( $search === array_intersect($v, $search) ) {
echo $k;
break;
}
}

@mickmackusa noticed it is safer to use array_intersect_assoc() here. He's right because when the multi-dimensional array items are unpredictable, nothing forbids to have items like that:

['miaou' => '50', 'graou' => 'Bob', 'score' => '50', 'name' => 'Bob']

where the searched values are also present but for other keys. In this case array_intersect() returns all correct values (with their corresponding keys of course) regardless the keys in $search, and the comparison with the search array will return false.

But using array_intersect_assoc(), you ensure that only values for keys in $search are taken in account.

Conclusion: If you let yourself be lulled into sleep by the seeming monotony of multidimensional array items, you won't be immune to surprise when unexpected variations arise.



Related Topics



Leave a reply



Submit