Php 将两个由相同id标记的数组合并在一起

Php 将两个由相同id标记的数组合并在一起,php,arrays,merge,Php,Arrays,Merge,将两个阵列组合在一起有一个问题。 一组是我的问题,另一组是答案 array\u merge()不起作用,因为我丢失了一些键+值。 另外,array\u intersect\u key()不是我想要的,我希望有人能帮我 两个数组都得到了键[questionid],这就是我要针对的键 数组1(答案) 还有一组问题: ( [0] => Array ( [questionId] =>

将两个阵列组合在一起有一个问题。 一组是我的问题,另一组是答案

array\u merge()
不起作用,因为我丢失了一些键+值。 另外,
array\u intersect\u key()
不是我想要的,我希望有人能帮我

两个数组都得到了键
[questionid]
,这就是我要针对的键

数组1(答案)

还有一组问题:

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

        )
最终阵列的外观必须是:

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] => 
        )

)

您可以开始为问题创建索引数组以查找它们。然后,您可以在答案上循环,并将问题数据添加到当前答案中:

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

如果要更新
$answers
数组,可以使用
&
通过引用更新
$answer

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


请注意,
questionid
questionid
不同,因此您将在组合数组中获得这两个键。您可以使用
unset()
删除其中一个,如中所示。

您可以开始为问题创建索引数组以查找它们。然后,您可以在答案上循环,并将问题数据添加到当前答案中:

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

如果要更新
$answers
数组,可以使用
&
通过引用更新
$answer

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


请注意,
questionid
questionid
不同,因此您将在组合数组中获得这两个键。您可以使用
unset()
删除其中一个,如。

使用简单的
foreach()
循环和
array\u search()
单向删除。在这里,数组搜索将在
$answers
数组中找到问题ID存在的位置,然后将问题和类型放到主
$questions
数组中

注意:我在答案数组中看到了带I大写字母的问题ID

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);
工作演示:

在不修改主阵列的情况下


工作演示:

使用简单的
foreach()
循环和
array\u search()。在这里,数组搜索将在
$answers
数组中找到问题ID存在的位置,然后将问题和类型放到主
$questions
数组中

注意:我在答案数组中看到了带I大写字母的问题ID

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);
工作演示:

在不修改主阵列的情况下


工作演示:

这一个对我来说很有效,但是否可以只制作一个新的数组,如$combineArray,而不是更改问题。@Gurdt延迟更新,但它应该可以工作,而无需使用另一个数组,如您所说的$combineArray来修改主数组。还添加了该工作演示。希望它能有所帮助:)这一个对我有用,但是否可以只制作一个像$combineArray这样的新数组,而不是修改问题。@Gurdt更新较晚,但它应该可以工作,而不需要使用另一个数组(如你所说的$combineArray)来修改主数组。还添加了该工作演示。希望它能帮上忙:)这个也行!谢谢你的快速反应,我想我会用这一个,因为我得到了一个组合阵列:)这一个也有效!谢谢你的快速反应,我想我会使用这个,因为我得到了一个组合阵列:)