0

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;
    }
Morten Hagh
  • 2,055
  • 8
  • 34
  • 66
  • can you give an example of a var_dump or print_r of what you expect? – Alain Tiemblo Sep 13 '12 at 11:47
  • What I am expecting is an output where the bricks (products) are randomly mixed. Lets say I have 10 different colored bricks where each bricks has an output of 10. The final image will then consist of 100 bricks in different colors randomly mixed. With my function above it will print 10 bricks of the same product, 10 bricks of the next and so on. It will be random which product comes first though. – Morten Hagh Sep 13 '12 at 12:12

2 Answers2

1

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.

MidnightLightning
  • 6,715
  • 5
  • 44
  • 68
  • This seems to work... Might need some tweaking, but it points me in the right direction! :) Thank you very much! – Morten Hagh Sep 13 '12 at 12:56
0

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.

raoulsson
  • 14,978
  • 11
  • 44
  • 68