I have Array #1 which contains:
Array
(
[attribute_pa_color] => blue
[attribute_pa_size] => large
)
I have Array #2 which contains:
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
)
)
How can I find the array key from Array #2 where the inner keys and values match Array 1's?
I have been experimenting with multiple foreach's but I can't seem to get this right, this is my current idea:
$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;
}
}
Arrays can be compared with ==
:
foreach ($array2 as $key => $item) {
if ($item == $array1) {
echo 'Item with key ' . $key;
}
}
由于array_search
接受针的数组甚至更容易:
$key = array_search($array1, $array2);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.