Use array_fill(), then merge them in. This is presuming both $qty and $products are both the same length.
<?php
$products = [ "4", "5", "8"];
$qty = ["1", "3", "4"];
$result = [];
foreach ($products as $key => $product) {
$result = array_merge(array_fill(0, $qty[$key], $product), $result);
}
print_r($result);
Result:
Array
(
[0] => 8
[1] => 8
[2] => 8
[3] => 8
[4] => 5
[5] => 5
[6] => 5
[7] => 4
)
https://3v4l.org/Rd4iR
Could also use a for loop:
<?php
$products = ["4", "5", "8"];
$qty = ["1", "3", "4"];
$result = [];
foreach ($qty as $key => $q) {
for ($i=0; $i < $q; $i++) {
$result[] = $products[$key];
}
}
print_r($result);