2015-10-06 73 views
0

我在多維數組的工作,我有一個這樣的數組,我想合併數組如何foreach循環PHP合併值

[0] => Array 
     (
      [QuizId] => 173 
      [QuizName] => Reprocessing Surgical Drapes and Gowns 
      [totalexams] => 1 
      [UserScore] => 8 
      [MaxScore] => 20 
      [passed] => 1 
      [CategoryId] => 1 
      [CategoryName] => CRCST 
      [totalTimes] => 1 
      [orderId] => 19 
      [productId] => 50 
     ) 

[1] => Array 
     (
      [QuizId] => 173 
      [QuizName] => Reprocessing Surgical Drapes and Gowns 
      [totalexams] => 1 
      [UserScore] => 8 
      [MaxScore] => 20 
      [passed] => 1 
      [CategoryId] => 1 
      [CategoryName] => CRCST 
      [totalTimes] => 1 
      [orderId] => 20 
      [productId] => 50 
     ) 

所有我需要的是使通過參加orderId的19,20陣列 即

[0] => Array 
     (
      [QuizId] => 173 
      [QuizName] => Reprocessing Surgical Drapes and Gowns 
      [totalexams] => 1 
      [UserScore] => 8 
      [MaxScore] => 20 
      [passed] => 1 
      [CategoryId] => 1 
      [CategoryName] => CRCST 
      [totalTimes] => 1 
      [orderId] => 19,20 
      [productId] => 50 
     ) 

我要像this.please安排幫助我實現這個

+0

刪除[什麼orderId]並存儲到列表中,然後用逗號分隔字符串並將其推送到其中一個數組。 –

+0

它有這樣的許多數組不僅兩個陣列最小100陣列 –

+0

一個for循環就足夠了... –

回答

1

你可以嘗試這樣的事情

//This is your old array that you describe in your first code sample 
$old_array = array(); 

// This will be the new joined array 
$new = array(); 

// This will hold the key-pairs for each array within your initial array 
$temp = array(); 

// This will hold all the values of orderId 
$orderId = array(); 

// Loop through each first-level array with the original array 
foreach($old_array as $val) { 
    //Loop through each second-level array 
    foreach($val as $key => $value) { 
     // Set the key-pair values in the $temp array 
     $temp[$key] = $temp[$value]; 

     if($key == "orderId") { 
      // Add the current orderId value to the orderId array 
      array_push($orderId,$value); 

      // Join all the orderId values into the $temp array 
      $temp[$key] = join(",", $orderId); 
     } 
    } 

} 

//Push the final values to the new array to get a 2 dimensional array 
array_push($new, $temp); 

注:我沒有測試任何下面的代碼,因此它很可能不是第一次合作。
這也是非常壞陣列設計,有更容易更方便,更實用的解決方案這個問題,但你需要給出更多的細節,你想達到

0
$original_array = array(); //this is the array you presented 

$new_array = array(); //this is the output array 

foreach($original_array as $arr) { 

    foreach($arr as $key => $value) { 

     if(array_key_exists($key, $new_array)) { //if you already assigned this key, just concat 
      $new_array[0][$key] .= "," . $value; 
     } else { //otherwise assign it to the new array 
      $new_array[0][$key] = $value; 
     } 
    } 
} 
0
 
It will give you the desired result 

$arrNew = array(); 
    $i = 0; 
    foreach($multiDArray as $array) 
    { 
     foreach($array as $key=>$value) 
     { 
      if($i > 0) 
      { 
       if($arrNew[$key] != $value) 
       { 
        $str = $arrNew[$key].','.$value; 
        $arrNew[$key] = $str; 
       } 
      } 
      else 
      { 
       $arrNew[$key] = $value; 
      }  
     } 
     $i++; 
    } 

    print_r($arrNew); 

Result: 
Array 
(
    [QuizId] => 173 
    [QuizName] => Reprocessing Surgical Drapes and Gowns 
    [totalexams] => 1 
    [UserScore] => 8 
    [MaxScore] => 20 
    [passed] => 1 
    [CategoryId] => 1 
    [CategoryName] => CRCST 
    [totalTimes] => 1 
    [orderId] => 19,20 
    [productId] => 1 
)