1

i have a questing regarding associates multidimensional arrays in php.

Let's say i have the following table:

Product Nr  Img
kat1    abc abc.png
kat1    xyz xyz.png
kat2    def def.png
kat2    gfh gfh.png
kat2    jug jug.png
kat3    lkj lkj.png
kat3    tfh tfh.png
kat4    fsg fsg.png
kat4    gzt gzt.png

Building an array like:

$table= array(
  array("Product" => "Kat1","Nr" =>"abc","IMG" =>"abc.png"),
  array("Product" => "Kat1","Nr" =>"xyz","IMG" =>"xyz.png"),
  array("Product" => "Kat2","Nr" =>"def","IMG" =>"def.png"),
  array("Product" => "Kat2","Nr" =>"gfh","IMG" =>"gfh.png"),
  array("Product" => "Kat2","Nr" =>"jug","IMG" =>"jug.png"),
  array("Product" => "Kat3","Nr" =>"lkj","IMG" =>"lkj.png"),
  array("Product" => "Kat3","Nr" =>"tfh","IMG" =>"tfh.png"),
  array("Product" => "Kat4","Nr" =>"fsg","IMG" =>"fsg.png"),
  array("Product" => "Kat4","Nr" =>"gzt","IMG" =>"gzt.png"),

  );

i want to built random productsets. and every productsets should contain one product from every single kategorie. for example the following output:

Kat1 = abc, abc.png
Kat2 = gfh, gfh.png
Kat3 = lkj, lkj.png
Kat4 = gzt, gzt.png

I'm not sure if i'm on the right way or if its possible with that kind of array. Any Ideas how i can solve that?

2
  • 2
    You are expected to try to write the code yourself. After doing more research if you have a problem post what you've tried with a clear explanation of what isn't working and provide a Minimal, Complete, and Verifiable example. Read How to Ask a good question. Be sure to take the tour and read this. Commented May 16, 2017 at 12:09
  • Can you please explain some more about what you exactly want? as you mentioned "Kat1 = abc, abc.png", is not much clear to identify your issue. Commented May 16, 2017 at 12:17

3 Answers 3

1

You can use this mysql query to get desired result:

SELECT 
    *
FROM
    (SELECT 
        *
    FROM
        product
    ORDER BY RAND()) AS shuffled_products
GROUP BY PRODUCT;

Once you received the result array in php using loop manipulate it to desired result.

Sign up to request clarification or add additional context in comments.

1 Comment

@swapfile this query is not optimized for performance. Hope you take care of that. Instead of * chose only the column you need.
1

1) Try rebuild your array in view like this (group by categories):

$tableGroupByCats = [
    "Kat1" => [
        ["Nr" =>"abc","IMG" =>"abc.png"],
        ["Nr" =>"xyz","IMG" =>"xyz.png"]
    ],
    "Kat2" => [
        ["Nr" =>"def","IMG" =>"def.png"],
        ["Nr" =>"gfh","IMG" =>"gfh.png"],
        ["Nr" =>"jug","IMG" =>"jug.png"]
    ],
    "Kat3" => [
        "Nr" =>"lkj","IMG" =>"lkj.png",
        "Nr" =>"tfh","IMG" =>"tfh.png"
    ],
    "Kat4" => [
        ["Nr" =>"fsg","IMG" =>"fsg.png"],
        ["Nr" =>"gzt","IMG" =>"gzt.png"]
    ]
];

following code will help you:

$table= array(
    array("Product" => "Kat1","Nr" =>"abc","IMG" =>"abc.png"),
    array("Product" => "Kat1","Nr" =>"xyz","IMG" =>"xyz.png"),
    array("Product" => "Kat2","Nr" =>"def","IMG" =>"def.png"),
    array("Product" => "Kat2","Nr" =>"gfh","IMG" =>"gfh.png"),
    array("Product" => "Kat2","Nr" =>"jug","IMG" =>"jug.png"),
    array("Product" => "Kat3","Nr" =>"lkj","IMG" =>"lkj.png"),
    array("Product" => "Kat3","Nr" =>"tfh","IMG" =>"tfh.png"),
    array("Product" => "Kat4","Nr" =>"fsg","IMG" =>"fsg.png"),
    array("Product" => "Kat4","Nr" =>"gzt","IMG" =>"gzt.png"),

);

$tableGroupByCats = [];
foreach ($table as $product) {
    $tableGroupByCats[$product['Product']][] = ["Nr" => $product["Nr"], "IMG" => $product["IMG"]];
}

Or you can retrieve array like that from DB using GROUP BY

2) Get your result:

$result = [];
foreach ($tableGroupByCats as $ctegory => $products) {
    $randNum = mt_rand(0, (count($products)-1));
    $result[$ctegory] = $products[$randNum];
}

Comments

1

Here's another way to do it:

Code:

shuffle($table);
foreach($table as $row){
    $result[$row["Product"]]="{$row["Product"]} = {$row["Nr"]}, {$row["IMG"]}"; // overwrite duplicates
}
ksort($result);  // sort result array by Product ASC
var_export($result);

Possible Output:

array (
  'Kat1' => 'Kat1 = xyz, xyz.png',
  'Kat2' => 'Kat2 = jug, jug.png',
  'Kat3' => 'Kat3 = lkj, lkj.png',
  'Kat4' => 'Kat4 = gzt, gzt.png',
)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.