2015-10-06 51 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 
)