2017-05-16 95 views
1

我有一個關於在php中關聯多維數組的任務。關聯數組PHP隨機值

比方說,我有如下表:

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 

構建陣列,如:

$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"), 

); 

我想建隨機productsets。每個產品組應包含來自每個單一分類的一種產品。例如下面的輸出:

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

我不知道如果我在正確的道路或者其可能與該類型的數組。任何想法如何我可以解決這個問題?

+2

預計你去嘗試自己**編寫代碼**。在[**做更多的研究**之後](https://meta.stackoverflow.com/q/261592/1011527)如果你有問題**發佈你已經嘗試了**的**明確的解釋不工作**並提供[最小,完整和可驗證示例](http://stackoverflow.com/help/mcve)。閱讀[如何問](http://stackoverflow.com/help/how-to-ask)一個很好的問題。請務必[參觀](http://stackoverflow.com/tour)並閱讀[this](https://meta.stackoverflow.com/q/347937/1011527)。 –

+0

你能解釋一些關於你到底想要什麼嗎?正如你提到的「Kat1 = abc,abc.png」,不太清楚你的問題。 – hvs9

回答

1

您可以使用此MySQL查詢來獲得期望的結果:

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

一旦你使用循環操縱它到期望的結果收到的結果數組在PHP。

+0

不錯的主意。我想這就是我將如何解決它。謝謝! – swapfile

+1

@swapfile此查詢未針對性能進行優化。希望你照顧好。而不是'*'只選擇你需要的列。 – gvgvgvijayan

1

1)嘗試重建陣列鑑於這樣的(按類別組):

$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"] 
    ] 
]; 

下面的代碼將幫助您:

$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"]]; 
} 

或者,你可以從數據庫使用檢索陣列一樣, GROUP BY

2)讓您的結果:

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

這裏是另一種方式來做到這一點:

代碼:

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

可能的輸出:

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