0

I am working in multidimensional array, i have an array like this and i want to merge array

[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
        )

All i need is to make array by join orderId 19,20 ie,

[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
        )

I want to arrange like this.please help me to achieve this

3
  • Remove [orderId] for each array and store in list, After that make a string with comma seperated and push to one of the array. Commented Oct 6, 2015 at 16:18
  • it has many arrays like this not only two array min 100 arrays Commented Oct 6, 2015 at 16:20
  • One for loop would be enough... Commented Oct 6, 2015 at 17:05

3 Answers 3

1

You can try something like this

//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);

Note: I did not test any of the following code so it is very likely to not work at first.
This is also VERY bad array design and there are more likely easier and more practical solutions to this problem, but you will need to give more details on what you want to achieve

Sign up to request clarification or add additional context in comments.

Comments

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;
        }
    }
}

Comments

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
)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.