I have a array list (for this example I'm using Googles colors). I'm wanting to be able to search for multiple key/value pairs if found the pair of array then return true otherwise return false.
Input Like below color and lenscolor pair : This value are dynamic comes from dynamic dropdown Array ( [Color] => Tokyo Tortoise,matte Gold & Green [LensColor] => Green )
In this array search above color and lenscolor pair This value are dynamic comes from xml file(converted in array) Array ( [0] => Array ( [Color] => Feathered Carmel,matte Gunmetal & Brown [LensColor] => Brown )
[1] => Array
(
[Color] => Matte Black,matte Black & Smoke
[LensColor] => Smoke
)
[2] => Array
(
[Color] => Tokyo Tortoise,matte Gold & Green
[LensColor] => Green
)
[3] => Array
(
[Color] => Matte Black,matte Black & Smoke
[LensColor] => Green
)
[4] => Array
(
[Color] => Feathered Carmel,matte Gunmetal & Brown
[LensColor] => Green
)
[5] => Array
(
[Color] => Feathered Carmel,matte Gunmetal & Brown
[LensColor] => Smoke
)
)
This is idea of would be, very simple:
$arr =Array(Array
(
"Color" => "Matte Black,matte Black & Smoke",
"LensColor" => "Smoke"
),
Array(
"Color" => "Tokyo Tortoise,matte Gold & Green",
"LensColor" => "Green"
),
Array(
"Color" => "Matte Black,matte Black & Smoke",
"LensColor" => "Green"
),
Array(
"Color" => "Feathered Carmel,matte Gunmetal & Brown",
"LensColor" => "Green"
),
Array(
"Color" => "Feathered Carmel,matte Gunmetal & Brown",
"LensColor" => "Smoke"
)
);
$founded = false;
$Color ="Tokyo Tortoise,matte Gold & Green";
$LensColor="Green";
echo $Color;
for ($i=0; $i<count($arr);$i++){
if($arr[$i]['Color']==$Color && $arr[$i]['LensColor']==$LensColor){
$founded=true;
break;
}
}
echo var_dump($founded);
It can make more sofisticated.
This is the direct implementation of an iterative search strategy:
<?php
$haystack = [
[
'Color' => 'Matte Black,matte Black & Smoke',
'LensColor' => 'Smoke'
],
[
'Color' => 'Tokyo Tortoise,matte Gold & Green',
'LensColor' => 'Green'
],
[
'Color' => 'Matte Black,matte Black & Smoke',
'LensColor' => 'Green'
],
[
'Color' => 'Feathered Carmel,matte Gunmetal & Brown',
'LensColor' => 'Green'
],
[
'Color' => 'Feathered Carmel,matte Gunmetal & Brown',
'LensColor' => 'Smoke'
]
];
$needle = [
'Color' => 'Tokyo Tortoise,matte Gold & Green',
'LensColor' => 'Green'
];
$result = false;
array_walk(
$haystack,
function ($entry) use ($needle, &$result) {
if ( ($entry['Color'] === $needle['Color'])
&& ($entry['LensColor'] == $needle['LensColor']) ) {
$result = true;
}
}
);
var_dump($result);
The output obviously is:
bool(true)
An alternative strategy is to filter the haystack:
<?php
$haystack = [
[
'Color' => 'Matte Black,matte Black & Smoke',
'LensColor' => 'Smoke'
],
[
'Color' => 'Tokyo Tortoise,matte Gold & Green',
'LensColor' => 'Green'
],
[
'Color' => 'Matte Black,matte Black & Smoke',
'LensColor' => 'Green'
],
[
'Color' => 'Feathered Carmel,matte Gunmetal & Brown',
'LensColor' => 'Green'
],
[
'Color' => 'Feathered Carmel,matte Gunmetal & Brown',
'LensColor' => 'Smoke'
]
];
$needle = [
'Color' => 'Tokyo Tortoise,matte Gold & Green',
'LensColor' => 'Green'
];
$result = array_filter(
$haystack,
function ($entry) use ($needle) {
return ( ($entry['Color'] === $needle['Color'])
&& ($entry['LensColor'] == $needle['LensColor']));
}
);
var_dump($result);
The output of that certainly is:
array(1) {
[1]=>
array(2) {
["Color"]=>
string(33) "Tokyo Tortoise,matte Gold & Green"
["LensColor"]=>
string(5) "Green"
}
}
UPDATE: In the comments to the question you now ask a more complex question stating that both the needles and the haystacks array elements are of dynamic nature, so that a matching based on fixed array entries is not possible. Though the exact matching criteria has not been defined I can give a short hint on what modifications probably point into the right direction:
The matching criteria has to be changed, easiest is to test the identity of the two arrays. So you use something like the following as comparison callback inside the iteration loop instead of above code:
function ($entry) use ($needle) {
return $entry === $needle;
}
That will only accept a match if the two arrays are identical in both their set of keys and the contained values.