I have 2 arrays that are combined into one. One array with some products and the other array with numbers (number of products).
$brick = "RT542,RT543,RT538";
$ratio = "10,15,13";
$bricks = explode(",", $brick);
$ratios = explode(",", $ratio);
$bricks_and_ratio = array_combine($bricks, $ratios);
Array (
[0] => RT542
[1] => RT543
[2] => RT538
)
Array (
[0] => 10
[1] => 15
[2] => 13
)
array_combine() then gives me this:
Array (
[RT542] => 10
[RT543] => 15
[RT538] => 13
)
So far so good. What I want is to shuffle this array in such way that I will get a row with first 2 x RT542 then 1 x RT538 then 3x RT543 and so forth and so on, up to the max number of items.
I am using this:
function BuildCustomBricks($myBricksAndRatios) {
$img = imagecreate(890,502);
imagealphablending($img, true);
imagesavealpha($img, true);
$keys = array_keys($myBricksAndRatios);
shuffle($keys);
$random = array();
foreach ($keys as $key) {
$random[$key] = $myBricksAndRatios[$key];
for($i = 1; $i <= $myBricksAndRatios[$key]; $i++) {
$cur = imagecreatefrompng("/var/www/brickmixer/bricks/". $key."-$i.png");
imagealphablending($cur, true);
imagesavealpha($cur, true);
imagecopy($img, $cur, -150+$i*132, 0, 0, 0, 125, 32);
}
imagedestroy($cur);
}
header('Content-Type: image/png');
imagepng($img);
}
It do shuffle but it creates a row of images of the same products, not in random order. I need to preserve the maximum number of products to each product key.
SOLUTION:
function shuffle_bricks($array) {
foreach($array as $key => $value) {
for($i = 1; $i <= $value; $i++) {
$new_array[] = $key;
}
}
shuffle($new_array);
return $new_array;
}