2011-11-21 69 views
0

我有一個2維陣列這樣一個如何組線2名維陣列的具有公共變量

$tab2 = Array 
(
    0 => Array 
     (
      "id_order" => 1551, 
      "firstname" => "ggg", 
      "lastname" =>" ggg", 
      "email" =>" var", 
      "ad1" =>" yjtyj", 
      "ad2" =>" ", 
      "adpostcode" => "ytjty", 
      "adcity" => "ytjy", 
      "adphone" => "cxgdfg", 
      "adphone_mobile" => "dfgd", 
      "dstateiso" => "", 
      "dstate" => "", 
      "dcountryiso" =>" FR", 
      "dcountry" => "France", 
      "aiother" => "", 
      "product_weight" => 15.02 
     ), 
    1 => Array 
     (
      "id_order" => 1551, 
      "firstname" => "ggg", 
      "lastname" =>" ggg", 
      "email" =>" var", 
      "ad1" =>" yjtyj", 
      "ad2" =>" ", 
      "adpostcode" => "ytjty", 
      "adcity" => "ytjy", 
      "adphone" => "cxgdfg", 
      "adphone_mobile" => "dfgd", 
      "dstateiso" => "", 
      "dstate" => "", 
      "dcountryiso" =>" FR", 
      "dcountry" => "France", 
      "aiother" => "", 
      "product_weight" => 35.02 
     ), 
    2 => Array 
     (
      "id_order" => 1551, 
      "firstname" => "ggg", 
      "lastname" =>" ggg", 
      "email" =>" var", 
      "ad1" =>" yjtyj", 
      "ad2" =>" ", 
      "adpostcode" => "ytjty", 
      "adcity" => "ytjy", 
      "adphone" => "cxgdfg", 
      "adphone_mobile" => "dfgd", 
      "dstateiso" => "", 
      "dstate" => "", 
      "dcountryiso" =>" FR", 
      "dcountry" => "France", 
      "aiother" => "", 
      "product_weight" => 43.02 
     ), 
    3 => Array 
     (
      "id_order" => 1550, 
      "firstname" => "dsgrg", 
      "lastname" => "regerzg", 
      "email" => "erger", 
      "ad1" => "5regrte", 
      "ad2" => "", 
      "adpostcode" => 62460, 
      "adcity" => "regger", 
      "adphone" => "", 
      "adphone_mobile" => "", 
      "dstateiso" => "", 
      "dstate" => "", 
      "dcountryiso" => "FR", 
      "dcountry" => "France", 
      "aiother" => "", 
      "product_weight" => 23.6 
     ), 
    4 => Array 
     (
      "id_order" => 1550, 
      "firstname" => "dsgrg", 
      "lastname" => "regerzg", 
      "email" => "erger", 
      "ad1" => "5regrte", 
      "ad2" => "", 
      "adpostcode" => 62460, 
      "adcity" => "regger", 
      "adphone" => "", 
      "adphone_mobile" => "", 
      "dstateiso" => "", 
      "dstate" => "", 
      "dcountryiso" => "FR", 
      "dcountry" => "France", 
      "aiother" => "", 
      "product_weight" => 13.6 
     ), 
    5 => Array 
     (
      "id_order" => 1549, 
      "firstname" =>" thtr", 
      "lastname" =>" rthr", 
      "email" => "[email protected]", 
      "ad1" =>"chetrhrthrtine", 
      "ad2" => "", 
      "adpostcode" => 84120, 
      "adcity" =>" rthrt", 
      "adphone" => "", 
      "adphone_mobile" => 2344235, 
      "dstateiso" => "", 
      "dstate" => "", 
      "dcountryiso" => "FR", 
      "dcountry" => "France", 
      "aiother" => "", 
      "product_weight" => 47.19 
     ) 
);  

即時試圖組誰在一個陣列相同id_order和計數陣列第二維產品重量的總和取決於出現次數。 我想要的輸出是這樣的

0 => Array 
     (
      "id_order" => 1551, 
      "firstname" => "ggg", 
      "lastname" =>" ggg", 
      "email" =>" var", 
      "ad1" =>" yjtyj", 
      "ad2" =>" ", 
      "adpostcode" => "ytjty", 
      "adcity" => "ytjy", 
      "adphone" => "cxgdfg", 
      "adphone_mobile" => "dfgd", 
      "dstateiso" => "", 
      "dstate" => "", 
      "dcountryiso" =>" FR", 
      "dcountry" => "France", 
      "aiother" => "", 
      "product_weight" => 93.06 //(total of the 3 occurences 15.02+35.02+43.02) 
     ), 
    1 => Array 
     (
      "id_order" => 1550, 
      "firstname" => "dsgrg", 
      "lastname" => "regerzg", 
      "email" => "erger", 
      "ad1" => "5regrte", 
      "ad2" => "", 
      "adpostcode" => 62460, 
      "adcity" => "regger", 
      "adphone" => "", 
      "adphone_mobile" => "", 
      "dstateiso" => "", 
      "dstate" => "", 
      "dcountryiso" => "FR", 
      "dcountry" => "France", 
      "aiother" => "", 
      "product_weight" => 37.2 
     ), 
    2 => Array 
     (
      "id_order" => 1549, 
      "firstname" =>" thtr", 
      "lastname" =>" rthr", 
      "email" => "[email protected]", 
      "ad1" =>"chetrhrthrtine", 
      "ad2" => "", 
      "adpostcode" => 84120, 
      "adcity" =>" rthrt", 
      "adphone" => "", 
      "adphone_mobile" => 2344235, 
      "dstateiso" => "", 
      "dstate" => "", 
      "dcountryiso" => "FR", 
      "dcountry" => "France", 
      "aiother" => "", 
      "product_weight" => 47.19 
     ) 
); 

我已經試過這個劇本,但它沒有工作

$nbr = sizeof($tab2); 
$x=0; 
for($i=0;$i<$nbr;$i++){ 
for($j=$i+1;$j<$nbr;$j++){ 


     if($tab[$i]["id_order"]== $tab[$j]["id_order"]) 
     { 
      echo 'doublon<br>'; 
      $tab[$i]["product_weight"] = $tab[$i]["product_weight"]+ $tab[$j]["product_weight"]; 
      // unset($tab[$j]); 
      array_splice($tab,$i); 
     } 
    } 
} 

感謝您的幫助

+0

如果您使用SQL獲取此數據,則應該使用它的組ping功能來完成這一點。 – simshaun

回答

0

我只想用一個foreach和索引上「id_order」列:

$newtab = array(); 
foreach ($tab2 as $i => $tab) { 
    if (!isset($newtab[$tab['id_order']])) { 
     $newtab[$tab['id_order']] = $tab; 
    } else { 
     $newtab[$tab['id_order']]['product_weight'] += $tab['product_weight']; 
    } 
}