如何从内部键和值与第一个数组匹配的第二个数组中找到数组键?
How can I find the array key from second array where the inner keys and values match first array?
我有数组 #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
)
)
如何从数组 #2 中找到数组键,其中内部键和值与数组 1 匹配?
我一直在尝试使用多个 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;
}
}
数组可以比较==
:
foreach ($array2 as $key => $item) {
if ($item == $array1) {
echo 'Item with key ' . $key;
}
}
因为 array_search
接受针数组,所以更容易:
$key = array_search($array1, $array2);
我有数组 #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
)
)
如何从数组 #2 中找到数组键,其中内部键和值与数组 1 匹配?
我一直在尝试使用多个 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;
}
}
数组可以比较==
:
foreach ($array2 as $key => $item) {
if ($item == $array1) {
echo 'Item with key ' . $key;
}
}
因为 array_search
接受针数组,所以更容易:
$key = array_search($array1, $array2);