如何在foreach循环php中合并值

时间:2015-10-06 16:12:05

标签: 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
        )

我只需要通过join 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
        )

我想像这样安排。请帮助我实现这个目标

3 个答案:

答案 0 :(得分: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);

注意:我没有测试以下任何代码,因此最初可能无法正常工作。
这也是非常糟糕的阵列设计,并且更容易和更实用的解决方案解决这个问题,但是你需要提供更多关于你想要实现的细节

答案 1 :(得分: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;
        }
    }
}

答案 2 :(得分: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
)