I have an array like this,
Array
(
[0] => Array
(
[0] => Array
(
[productId] => 145
[variantId] => 179
)
[1] => Array
(
[productId] => 145
[variantId] => 180
)
[2] => Array
(
[productId] => 147
[variantId] => 181
)
[3] => Array
(
[productId] => 147
[variantId] => 182
)
)
[1] => Array
(
[0] => Array
(
[productId] => 147
[variantId] => 181
)
[1] => Array
(
[productId] => 147
[variantId] => 184
)
)
[2] => Array
(
[0] => Array
(
[productId] => 145
[variantId] => 178
)
[1] => Array
(
[productId] => 145
[variantId] => 180
)
[2] => Array
(
[productId] => 147
[variantId] => 181
)
[3] => Array
(
[productId] => 147
[variantId] => 185
)
[4] => Array
(
[productId] => 147
[variantId] => 186
)
)
)
For this input we can see that [productId] => 147,[variantId] => 181
is common an all index of array.
Im using foreach
to check this. But my code is working fine for 2 index, but not for more than 2 index.
In foreach
im taking 0th
index and comparing with other index, but not working.
Here is the code i have used so far.
$first = $name1[0];
$a = array();
unset($name1[0]);
foreach ($name1 as $row) {// foreach main array
foreach ($first as $row1) {// foreach first index of array array
foreach ($row as $kkk => $r1) {//foreach index of main array
// if first array's index's product is is equal to main array index's product id, push values to one array
if ($row1['productId'] == $r1['productId']) {
if (isset($row1['variantId']) && isset($r1['variantId'])) {
if ($row1['variantId'] == $r1['variantId']) {
$a[] = $r1;
}
} else {
$a[] = $r1;
}
}
}
}
}