Question

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;
    }
Was it helpful?

Solution

Haven't tested this,but it ought to get you on the right track:

<?php
function shufflebricks($bricks) {
  $rs = array();
  while (count($bricks) >= 0) {
    $key = array_rand($bricks, 1);
    $bricks[$key]--; // Use one brick
    $rs[] = $key; // Add it to output
    if ($bricks[$key] <= 0) unset($bricks[$key]); // Remove if there's no more of this brick
  }
  return $rs;
}
?>

This uses one brick at a time from a random brick type that has bricks left. If you want to use a chunk at a time, add a $quantity = rand(1, $bricks[$key]); in there.

OTHER TIPS

If you use indexed array, make sure to normalize indexes as well:

function shuffleArray($source) {
    $target = array();
    for($i = count($source); $i > 0; $i--) {
        $key = rand(0, $i - 1);
        $target[] = $source[$key];
        unset($source[$key]);
        $source = array_values($source);
    }
    return $target;
}

Happens through the array_values function.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top