I need to write a function that searches the two-tier board according to certain parameters, which works, but I question whether there is a simpler, lighter way to perform this task.
<?php
function findValue(array $array, array $parameters, $multipleResoult = false){
$result = array();//used when $multipleResoult == true
$suspicious = false;
foreach($array as $childArray){
foreach($parameters as $k => $p){
if(array_key_exists($k,$childArray)){
if($childArray[$k] == $p){
$suspicious = $childArray;
} else {
$suspicious = false;
continue 2;
}
} else {
$suspicious = false;
continue 2;
}
}
if(is_array($suspicious)){
$result[] = $suspicious;
if($multipleResoult == true){
$suspicious = false;
} else {
break;
}
}
}
return $result;
}
$arr = Array
(
0 => Array
(
"id" => 1,
"id_shop" => 1,
"id_lang" => 1,
"id_product" => 1,
"id_field" => 3,
"field_value" => "zxczxc"
),
// find if single and multiple
1 => Array
(
"id" => 2,
"id_shop" => 1,
"id_lang" => 2,
"id_product" => 1,
"id_field" => 3,
"field_value" => "sdfsdfsdf"
),
2 => Array
(
"id" => 3,
"id_shop" => 1,
"id_lang" => 2,
"id_product" => 2,
"id_field" => 3,
"field_value" => "sdfsdfsdf"
),
// find if multiple
3 => Array
(
"id" => 3,
"id_shop" => 1,
"id_lang" => 2,
"id_product" => 1,
"id_field" => 3,
"field_value" => "sdfsdfsdf"
)
);
echo '-----------------SINGLE----------------------';echo "\n";echo '<pre>';
print_r(findValue($arr, array(
'id_shop' => 1,
'id_field' => 3,
'id_lang'=> 2,
'id_product' => 1)
));
echo '-----------------MULTIPLE----------------------';echo "\n";echo '<pre>';
print_r(findValue($arr, array(
'id_shop' => 1,
'id_field' => 3,
'id_lang'=> 2,
'id_product' => 1)
,true));
?>