我正在创建一个搜索功能,其中需要查找来自两个数组的匹配结果$array1
是我在$array2
执行搜索的关键字集,这是多维数组
数组示例:
$array1 = ['Agriculture & Farming','Modern','Monograms'];
$array2 = [[1,"Agriculture & Farming","Bold","000"],[2,"Agriculture & Farming","Bold","f44336"],[3,"Agriculture & Farming","Bold","E91E63"],[4,"Agriculture & Farming","Bold","9C27B0"],[5,"Agriculture & Farming","Bold","673AB7"],[6,"Agriculture & Farming","Bold","3F51B5"],[7,"Agriculture & Farming","Bold","2196F3"];
我需要从$ arrays2中找到匹配的关键字项ID,但它不起作用 我循环遍历两个阵列并匹配,但显示错误的结果
这就是我现在的尝试,但它给出了错误的结果:
foreach($array2 as $itemkey) {
$k1 = $itemkey[1];
$k2 = $itemkey[2];
$k3 = $itemkey[3];
$ResutItem= $item;
foreach($array1 as $searckKey ) {
if ($searckKey == $k1 || $searckKey == $k2 || $searckKey == $k3) {
echo implode(" ", $ResutItem).
'<br>';
}
}
}
答案 0 :(得分:0)
您可以使用array_intersect 这将为您提供一个数组,其中键位于array2的子阵列中 值是匹配值。
Foreach($array2 as $key => $arr){
$match[$key] = array_merge([$array2[$key][0]], array_intersect($arr, $array1));
}
Var_dump($match);