Php 洗牌数组键,然后在foreach中混合

Php 洗牌数组键,然后在foreach中混合,php,arrays,random,foreach,gdlib,Php,Arrays,Random,Foreach,Gdlib,我有两个数组,它们组合成一个。一个数组包含一些产品,另一个数组包含数字(产品数量) array_combine()则给出以下结果: Array ( [RT542] => 10 [RT543] => 15 [RT538] => 13 ) 到目前为止还不错。我想要的是以这样的方式洗牌这个数组,我将得到一行,首先是2 x RT542,然后是1 x RT538,然后是3 x RT543,以此类推,直到最大项数 我用的是: function BuildCustomBricks(

我有两个数组,它们组合成一个。一个数组包含一些产品,另一个数组包含数字(产品数量)

array_combine()则给出以下结果:

Array ( 
[RT542] => 10 
[RT543] => 15 
[RT538] => 13 
)
到目前为止还不错。我想要的是以这样的方式洗牌这个数组,我将得到一行,首先是2 x RT542,然后是1 x RT538,然后是3 x RT543,以此类推,直到最大项数

我用的是:

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);
    }  
函数BuildCustomBricks($myBricksAndRatios){
$img=imagecreate(890502);
imagealphablending($img,true);
imagesavealpha($img,true);
$keys=数组_键($myBricksAndRatios);
洗牌($键);
$random=array();
foreach($key作为$key){
$random[$key]=$mybricksandrations[$key];
对于($i=1;$i$value){

对于($i=1;$i来说,我们还没有对此进行测试,但它应该会让您走上正确的道路:

<?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;
}
?>

如果使用索引数组,请确保也规范化索引:

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;
}

通过
array\u values
函数发生。

您能给出一个变量转储或打印您期望的内容的示例吗?我期望的是砖块(产品)的输出是随机混合的。假设我有10块不同颜色的砖块,每个砖块有10个输出。最终的图像将由100块不同颜色的砖块随机混合而成。通过上面的功能,它将打印同一产品的10块砖块,下一个产品的10块,依此类推。这将是随机的,哪种产品是第一个。这看起来像是s工作…可能需要一些调整,但它为我指明了正确的方向!:)非常感谢!
<?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;
}
?>
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;
}