Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8544665
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T12:36:55+00:00 2026-06-11T12:36:55+00:00

I have 2 arrays that are combined into one. One array with some products

  • 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;
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T12:36:57+00:00Added an answer on June 11, 2026 at 12:36 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the below arrays that I have combined into one. The second array,
I have 3 byte arrays in C# that I need to combine into one.
I have a 2D array that I have split up into several 1D arrays
I have 2 arrays that I would like to render in a template, one
I have two arrays of data that I'm trying to amalgamate. One contains actual
Assume that we have multiple arrays of integers. You can consider each array as
Assume that I have two arrays as follow: $array1 = array(1, 3, 5); $array2
So numpy has some convenience functions for combining several arrays into one, e.g. hstack
Android: I have two byte arrays one byte array of bitmap and another byte
I have a database that has a table called products, and into the table

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.