2017-05-16 1 views
1

나는 PHP에서 연관 다차원 배열에 관한 questing 있습니다.연관 배열 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 

같은 배열을 건물 :

의 내가 다음과 같은 테이블이 있다고 가정 해 봅시다 내가 임의 productsets을 구축 할

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

); 

. 모든 제품 세트에는 모든 단일 카테고리의 제품이 하나 있어야합니다. 예를 들어 다음과 같은 출력 :

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

난 내가 배열의 종류와 올바른 방법에 또는 가능하면 해요 있는지 확실하지 않습니다. 어떤 생각을 어떻게 풀 수 있습니까?

+2

당신은 ** ** 코드를 직접 작성하려고 할 것으로 예상된다. [** 더 많은 연구를하고 **] (https://meta.stackoverflow.com/q/261592/1011527) 문제가 있다면 ** 당신이 시도한 것을 게시하십시오 ** 명확한 설명과 함께 ** ** 작동하지 않으며 [Minimal, Complete, Verifiable example] (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에서 결과 배열을받은 후.

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

은 다음 코드는 당신을 도울 것입니다 :

+0

좋은 아이디어. 나는 그것을 어떻게 해결할 것인가라고 생각한다. 감사! – swapfile

+1

@swapfile이 쿼리는 성능을 위해 최적화되지 않았습니다. 당신이 그 일을 처리하기를 바랍니다. '*'대신 필요한 컬럼 만 선택했다. – gvgvgvijayan

1

1)이 같은 관점에서 카테고리별로 (그룹) 배열을 다시 시도

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

또는 사용하여 DB에서 같은 배열을 검색 할 수 있습니다

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

2) BY GROUP은 결과를 얻을

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', 
)