0

I have a multidimensional array:

$array =
  Array ( 
  [0] => Array ( [id] => 2 [zoneId] => 2 [buildingId] => 2 [typeId] => 2 ) 
  [1] => Array ( [id] => 4 [zoneId] => 2 [buildingId] => 2 [typeId] => 1 ) 
  [2] => Array ( [id] => 6 [zoneId] => 6 [buildingId] => 17 [typeId] => 2 ) )

And I would like to search if the combination of, for example, [buildingId] => 2, [typeId] => 2 exists is array 0, 1 or 2.

I tried the following:

$keyType = array_search(2, array_column($array, 'typeId'));
$keyBuilding = array_search(2, array_column($array, 'buildingId'));

if(is_numeric($keyType)&&is_numeric($keyBuilding)){
     echo 'Combination does exists'
}

This works, but gives also a false positive if I would search for [buildingId] => 17, [typeId] => 1. How can I solve this?

edit

I would also like to know if a combination is not in the array, how can I arrange that?

if($result == false){
echo 'does not exists';
}
0

4 Answers 4

1

You can try this code:

$keyTypeExistsAndHaveSameValue = isset($array['typeId']) && $array['typeId'] === 2;
$keyBuildingExistsAndHaveSameValue = isset($array['buildingId']) && $array['buildingId'] === 2;

if($keyTypeExistsAndHaveSameValue && $keyBuildingExistsAndHaveSameValue){
     echo 'Combination does exists'
}

This code check if typeId & buildingId keys exist but it also check if its values are 2 and 2.

Sign up to request clarification or add additional context in comments.

Comments

0
$buildingId = 2;
$typeId = 2;
$result = false;
foreach ($array as $key => $val) {
    if ($val['buildingId'] == $buildingId && $val['typeId'] == $typeId) {
        $result = $key; // If you want the key in the array
        $result = $val; // If you want directly the entry you're looking for
        break; // So that you don't iterate through the whole array while you already have your reuslt
    }
}

2 Comments

if I would like to know that combination typeId = 17 and buildingId = 1 does not exists, how would I do that?
@LoesVisser If no rsult is found, $result evaluates to false after the foreach.
0

I think what you need is this

$keyType = array_search(1, array_column($array, 'typeId'));
$keyBuilding = array_search(17, array_column($array, 'buildingId'));

if(is_numeric($keyType)||is_numeric($keyBuilding)){
  echo 'Combination does exists';
}  

Now here you need or operator instead of and operator because you want either typeid = 1 exists or building id = 17 exists.

If I have understand your question correctly then you are trying to do something like this, right ?

Hope this helps!

Comments

0

You would need to do a foreach loop to get the actual array number, the other solution don't seems to answer what you're looking for, which is to get the index number.

I would like to search if the combination of, for example, [buildingId] => 2, [typeId] => 2 exists is array 0, 1 or 2.

EDIT: This code is just sample code to show how you would get the array index number, in a production environment you would save the matching arrays, comparison figures are not hard coded, etc...

$array = array(array('id' => 2, 'zoneId' => 2, 'buildingId' => 2, 'typeId' => 2),
            array('id' => 4, 'zoneId' => 2, 'buildingId' => 2, 'typeId' => 2),
            array('id' => 6, 'zoneId' => 6, 'buildingId' => 17, 'typeId' => 2));


foreach ($array as $building => $building_details)
{
    if ($building_details['buildingId'] === 2 && $building_details['typeId'] === 2)
    {
        echo 'Array number ' . $building . ' matches criteria<br>';
    }
}

Output:

Array number 0 matches criteria
Array number 1 matches criteria

You can view this snippet online here.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.