I have two arrays that have to be migrated.
$products = array(
array(
'title' => 'Product #1',
'sku' => 53159,
'price' => 12,
'image' => 'product_01.jpg'
),
array(
'title' => 'Product #2',
'sku' => 60290,
'price' => 12,
'image' => 'product_01.jpg'
),
array(
'title' => 'Product #3',
'sku' => 24195,
'price' => 12,
'image' => 'product_01.jpg'
)
);
$similar_products = array(
array(25195, 53159),
array(60290, 33619),
array(80001, 24195)
);
I wan't to build a final array with the following result:
$final= array(
array(
'title' => 'Product #1',
'sku' => 53159,
'similar' => 25195,
'price' => 12,
'image' => 'product_01.jpg'
),
array(
'title' => 'Product #2',
'sku' => 60290,
'similar' => 33619,
'price' => 12,
'image' => 'product_01.jpg'
),
array(
'title' => 'Product #3',
'sku' => 24195,
'similar' => 80001,
'price' => 12,
'image' => 'product_01.jpg'
)
);
I tried to find the similar skus with in_array inside a foreach loop:
foreach ( $products as $product ) {
foreach ( $similar_products as $similar_product) {
if ( in_array( $product['sku'] ,$similar_product) ) {
// Build the $final array here
}
}
}
but it's taking too long.
I there any other method to help me getting faster the similar sku?