使用array_merge_recursive合并2个数组不能正常工作

时间:2021-05-26 21:14:08

My database has 2 columns. the person's ID is repeated 5 times in 1 column and each its paired in another column with a question answer Something like this:

我的数据库有2列。该人的ID在1列中重复5次,并且每个人在另一列中配对并提出问题答案如下:

ID  Answer
1   A1
1   A4
1   A2
1   A9
1   A3
12  A1
12  A11
12  A12
12  A17
12  A2

What i want to try to do is to merge all the answers into 1 array with its ID something like

我想要尝试做的是将所有答案合并为1个数组,其ID类似于

array (
        [1] => array ( 0 => 'A1', 1 => 'A4', 2 => 'A2', 3 => 'A9', 4 => 'A3'),
        [12] => array ( 0 => 'A1', 1 => 'A11', 2 => 'A12', 3 => 'A17', 4 => 'A2')
        )

My code is as follows:

我的代码如下:

foreach ($quiz_answers as $aq => $aa)
            {
                $array_loop = array(  $aa['response_id'] => array( $aa['answer'] ) );
                $ss = array_merge_recursive($array_loop, $array_loop);

            }

My Problem is that somehow the loop doesnt merge in the desired way and i only get 2 outputs. I am not very good at manipulating arrays and probably i need another function but i am not quite sure what i am missing. I've tried using another variable in array_merge_recursive($anotherVariable, $array_loop); but this doesnt work either.

我的问题是,循环不会以所需的方式合并,我只得到2个输出。我不是很擅长操纵数组,可能我需要另一个功能,但我不太清楚我缺少什么。我尝试在array_merge_recursive中使用另一个变量($ anotherVariable,$ array_loop);但这也不起作用。

4 个解决方案

#1


2  

Simply change your foreach loop to construct the resulting array how you desire.

只需更改foreach循环,即可根据需要构建生成的数组。

foreach ($quiz_answers as $aa) {
    $ss[$aa['response_id']][] = $aa['answer'];
}

This gives an $ss array as you want:

这可以根据需要提供$ ss数组:

array(
    1  => array('A1', 'A4', 'A2', 'A9', 'A3'),
    12 => array('A1', 'A11', 'A12', 'A17', 'A2'),
)

#2


0  

What you need to do is to have an array that holds the user ids, each of which is an associative array that holds an array of answers.

你需要做的是拥有一个包含用户id的数组,每个数组都是一个包含一组答案的关联数组。

array (
        [1] => array ( 0 => 'A1', 0 => 'A4', 0 => 'A2', 0 => 'A9', 0 => 'A3'),
        [12] => array ( 0 => 'A1', 0 => 'A11', 0 => 'A12', 0 => 'A17', 0 => 'A2')
        )

so something like this will work (skeleton code below)

所以这样的东西会起作用(下面的骨架代码)

$answers = array();
foreach( $quiz_answers as $id => $ans ) {
  // check if $answers[$id] exists, otherwise create it here....
  if(exists(....)) ...
  else $answers[$id] = array();

  // Then add the current answer to it, as below
  array_push($answers[$id], $ans );
}

#3


0  

echo "<pre />";
for($i=0;$i<count($array);$i++)
{
    $output[$array[$i]['ID']][]=$array[$i]['Answer'];
}
print_r($output);

#4


0  

$quiz_answers = // get all the data from database.
$res          = array();
foreach($quiz_answers as $key=>$val){
     $res[$val['response_id']][]    = $val['answer'];
}

print_r($res);

#1


2  

Simply change your foreach loop to construct the resulting array how you desire.

只需更改foreach循环,即可根据需要构建生成的数组。

foreach ($quiz_answers as $aa) {
    $ss[$aa['response_id']][] = $aa['answer'];
}

This gives an $ss array as you want:

这可以根据需要提供$ ss数组:

array(
    1  => array('A1', 'A4', 'A2', 'A9', 'A3'),
    12 => array('A1', 'A11', 'A12', 'A17', 'A2'),
)

#2


0  

What you need to do is to have an array that holds the user ids, each of which is an associative array that holds an array of answers.

你需要做的是拥有一个包含用户id的数组,每个数组都是一个包含一组答案的关联数组。

array (
        [1] => array ( 0 => 'A1', 0 => 'A4', 0 => 'A2', 0 => 'A9', 0 => 'A3'),
        [12] => array ( 0 => 'A1', 0 => 'A11', 0 => 'A12', 0 => 'A17', 0 => 'A2')
        )

so something like this will work (skeleton code below)

所以这样的东西会起作用(下面的骨架代码)

$answers = array();
foreach( $quiz_answers as $id => $ans ) {
  // check if $answers[$id] exists, otherwise create it here....
  if(exists(....)) ...
  else $answers[$id] = array();

  // Then add the current answer to it, as below
  array_push($answers[$id], $ans );
}

#3


0  

echo "<pre />";
for($i=0;$i<count($array);$i++)
{
    $output[$array[$i]['ID']][]=$array[$i]['Answer'];
}
print_r($output);

#4


0  

$quiz_answers = // get all the data from database.
$res          = array();
foreach($quiz_answers as $key=>$val){
     $res[$val['response_id']][]    = $val['answer'];
}

print_r($res);