Простейшая модификация:
$data = [];
foreach ($products as $product) {
$temp = [
'product_id' => $product['product_id'],
'name' => $product['name'],
'price' => $price,
'special' => $special,
'tax' => $tax,
'minimum' => $product['minimum'] > 0 ? $product['minimum'] : 1,
'rating' => $product['rating'],
];
$cart_products = $this->cart->getProducts();
foreach ($cart_products as $cart_product) {
if ($cart_product['id'] == $product['product_id']) {
$temp['quantity'] = $cart_product['quantity'] > 0 ? $cart_product['quantity'] : 1;
}
}
$data['products'][] = $temp;
}
Но я бы изменил логику:
$cart_products = $this->cart->getProducts();
// если ключи массива - уже столбец id, строчка не нужна
$cart_products = array_combine(array_column($cart_products, 'id'), $cart_products);
$data = [];
foreach ($products as $product) {
$data['products'][] = [
'product_id' => $product['product_id'],
'name' => $product['name'],
'price' => $price,
'special' => $special,
'tax' => $tax,
'minimum' => $product['minimum'] > 0 ? $product['minimum'] : 1,
'rating' => $product['rating'],
'quantity' => $cart_products[$product['product_id']]['quantity'] > 0 ? $cart_products[$product['product_id']]['quantity'] : 1
];
}
P.S. Можно и дальше экспериментировать с кодом:
$cart_products = $this->cart->getProducts();
$cart_products = array_combine(array_column($cart_products, 'id'), array_column($cart_products, 'quantity'));
$data = [];
foreach ($products as $product) {
$data['products'][] = [
'product_id' => $product['product_id'],
'name' => $product['name'],
'price' => $price,
'special' => $special,
'tax' => $tax,
'minimum' => $product['minimum'] > 0 ? $product['minimum'] : 1,
'rating' => $product['rating'],
'quantity' => $cart_products[$product['product_id']] > 0 ? $cart_products[$product['product_id']] : 1
];
}
И это ещё не предел упрощения основного цикла...