简体   繁体   中英

Merge two arrays together tagged by same id

I have a problem to combine two arrays together. One array I've got the Questions and the Other are the answers.

array_merge() doesn't work because than I lost some keys + value's. Also array_intersect_key() doesn't what I want, I hope someone can help me out.

Both array got key [questionid] that's the key I want to target on.

Array 1 (Answers);

Array
(
    [0] => Array
        (
            [questionid] => 28f68dc4-acc6-4ab7-956d-ca41abf3a4c2
            [answer] => Antwoord 89
            [extra] => 
        )

    [1] => Array
        (
            [questionid] => 28f-28f68dc4-acc6-4ab7-956d-ca41abf3a4c20
            [answer] => Antwoord 20
            [extra] => Extra informatie 20
        )

    [2] => Array
        (
            [questionid] => d8f-28f68dc4-acc6-4ab7-956d-ca41abf3a4c20
            [answer] => Antwoord 3
            [extra] => 
        )

)

And an array with the questions:

       (
            [0] => Array
                (
                    [questionId] => 28f-28f68dc4-acc6-4ab7-956d-ca41abf3a4c20
                    [question] => 1. Question 1
                    [type] => typequestion/question-text-input
                )

        )

How the final array must look like is:

Array
(
    [0] => Array
        (
            [questionid] => 28f68dc4-acc6-4ab7-956d-ca41abf3a4c2
            [answer] => Antwoord 89
            [extra] => 
        )

    [1] => Array
        (
            [questionid] => 28f-28f68dc4-acc6-4ab7-956d-ca41abf3a4c20
            [answer] => Antwoord 20
            [extra] => Extra informatie 20
            [question] => 1. Question 1
            [type] => typequestion/question-text-input
        )

    [2] => Array
        (
            [questionid] => d8f-28f68dc4-acc6-4ab7-956d-ca41abf3a4c20
            [answer] => Antwoord 3
            [extra] => 
        )

)

You could start to create an indexed array for the question to find them. Then, you could loop over the answers and add question data to current answer:

$indexedQuestions = [];
foreach ($questions as $question) {
    $id = $question['questionId'];
    $indexedQuestions[$id] = $question;
}

$combined = [];
foreach ($answers as $answer) {
    $id = $answer['questionid'];
    if (isset($indexedQuestions[$id])) {
        $combined[] = $answer + $indexedQuestions[$id];
    } else {
        $combined[] = $answer;
    }
}

See example

If you want to update the $answers array, you can use & to update $answer by reference.

foreach ($answers as &$answer) {
    $id = $answer['questionid'];
    if (isset($indexedQuestions[$id])) {
        $answer += $indexedQuestions[$id];
    }
}

See example

Note that questionid is different that questionId , so you will get both key in combined arrays. You can use unset() to remove one of them, like in this example .

One way with simple foreach() loop and array_search() . Here array search will find on $answers array where the questionId exists and then it will put the question and type to the main $questions array.

Note: I see questionId with I in capital on answers array

foreach($questions as $key=>$value){
    $k = array_search($value['questionid'], array_column($answers, 'questionId'));
    if($k!==FALSE)){
        $questions[$key]['question'] = $answers[$k]['question'];
        $questions[$key]['type'] = $answers[$k]['type'];
    }
}
print_r($questions);

WORKING DEMO: https://3v4l.org/O4uDc

Without modifying main array ,

$combineArray = [];
foreach($questions as $key=>$value){
    $combineArray[$key] = $value;
    $k = array_search($value['questionid'], array_column($answers, 'questionId'));
    if($k!==FALSE){
        $combineArray[$key]['question'] = $answers[$k]['question'];
        $combineArray[$key]['type'] = $answers[$k]['type'];
    }
}
print_r($combineArray);

Working Demo: https://3v4l.org/9vhla

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM