我有一个数组#1,其中包含:
Array
(
[attribute_pa_color] => blue
[attribute_pa_size] => large
)
我有2号数组,其中包含:
Array
(
[4624] => Array
(
[attribute_pa_color] => blue
[attribute_pa_size] => large
)
[4625] => Array
(
[attribute_pa_color] => blue
[attribute_pa_size] => medium
)
[4626] => Array
(
[attribute_pa_color] => blue
[attribute_pa_size] => small
)
)
如何在内部键和值与数组1匹配的数组#2中找到数组键?
我一直在尝试使用多个foreach,但是似乎无法正确实现,这是我目前的想法:
$i = 0;
foreach( $array_2 as $array2_key => $array2_array ) {
foreach( $array2_array as $a2_key => $a2_value ) {
if( $a2_value == $array1[$a2key] ) {
$i = $i + 1;
if( $i == count( $array1 ) ) {
$break = 1;
}
if( $break == 1 ) {
break;
}
}
}
if( $break == 1 ) {
echo 'key is: ' . $array2_key;
break;
}
}
答案 0 :(得分:3)
可以将数组与==
进行比较:
foreach ($array2 as $key => $item) {
if ($item == $array1) {
echo 'Item with key ' . $key;
}
}
答案 1 :(得分:1)
array_search
接受针的数组更容易:
$key = array_search($array1, $array2);