2010-08-12 73 views
3

如何組這樣的:合併和組陣列數據

Array(
     [0] => Array(
        [brand] => 'ABC', 
        [model] => 'xyz', 
        [size] => 13 
    ) 
     [1] => Array(
        [brand] => 'QWE', 
        [model] => 'poi', 
        [size] => 23 
    ) 
     [2] => Array(
        [brand] => 'ABC', 
        [model] => 'xyz', 
        [size] => 18 
    )) 

成這樣:

Array(
     [0] => Array(
        [brand] => 'ABC', 
        [model] => 'xyz', 
        [size] => Array(
           13, 
           18 
          ) 
    ) 
     [1] => Array(
        [brand] => 'QWE', 
        [model] => 'poi', 
        [size] => 23 
    )) 

我想組由品牌和型號,但插入的大小的數組,因爲這一個不一樣。

+1

你能用句子解釋你想做什麼嗎? – 2010-08-12 15:03:06

+0

我想按BRAND和MODEL分組,但在SIZE中插入一個數組,因爲這個不一樣。 – Derick 2010-08-12 15:12:41

回答

3

在算法方面,你只需要:

  1. 創建一個空數組。

  2. 掃描源數組中的每個數組元素,爲每個遇到的新品牌/模型創建一個新元素(在空數組中)並添加大小子數組。

  3. 如果已經有品牌/型號條目,只需將其大小添加到子數組中,如果它尚不存在。

您可以實現此如下(粗糙,但它的工作原理):

<?php 
    // Test data. 
    $sourceArray = array(array('brand'=>'ABC', 'model'=>'xyz', 'size'=>13), 
         array('brand'=>'QWE', 'model'=>'poi', 'size'=>23), 
         array('brand'=>'ABC', 'model'=>'xyz', 'size'=>18), 
         ); 
    $newArray = array(); 

    // Create a new array from the source array. 
    // We'll use the brand/model as a lookup. 
    foreach($sourceArray as $element) { 

     $elementKey = $element['brand'] . '_' . $element['model']; 

     // Does this brand/model combo already exist? 
     if(!isset($newArray[$elementKey])) { 
      // No - create the new element. 
      $newArray[$elementKey] = array('brand'=>$element['brand'], 
              'model'=>$element['model'], 
              'size'=>array($element['size']), 
              ); 
     } 
     else { 
      // Yes - add the size (if it's not already present). 
      if(!in_array($element['size'], $newArray[$elementKey]['size'])) { 
       $newArray[$elementKey]['size'][] = $element['size']; 
      } 
     } 
    } 

    // *** DEBUG *** 
    print_r($newArray); 
?> 

另外,爲便於訪問的我做了它,這樣大小的子陣列始終是一個數組。 (即:你沒有允許它可能只是一個元素。)

+1

儘管如果品牌/型號已經存在,它可能應該是'if(!in_array($ element ['size'],$ newArray [$ elementKey] ['size']))'只添加新尺寸:p。 – wimvds 2010-08-12 15:24:04

+1

哇!非常感謝你!你救了我的一天!來自日本的歡呼! – Derick 2010-08-12 15:25:20

+1

@Derick - 沒問題。歡迎來到stackoverflow。 :-) – 2010-08-12 15:29:26

0
//$array is the array in your first example. 

foreach($array as $item) { 
    $itemname = $item["brand"] . "_" . $item["model"] 

    $new_array[$itemname]["brand"] = $item["brand"]; 
    $new_array[$itemname]["model"] = $item["model"]; 
    $new_array[$itemname]["size"][] = $item["size"]; 
} 
+0

這將始終添加大小,即使它是重複的。 – 2010-08-12 15:17:56

+0

也是一個選項!感謝您的幫助! – Derick 2010-08-12 15:28:00

0

「升級」到knarf的片斷....

foreach($array as $item) { 
    $itemname = $item["brand"] . "_" . $item["model"] 

    $new_array[$itemname]["brand"] = $item["brand"]; 
    $new_array[$itemname]["model"] = $item["model"]; 
    $new_array[$itemname]["size"][ $item["size"] ] = 1; 
} 

foreach($new_array as $itemname=>$data) { 
    if(isset($data['size']) && is_array($data['size'])) { 
    $new_array[$itemname]['size']=array_keys($new_array[$itemname]['size']); 
    } 
} 

沒有重複了...