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

我的数据库有2列。 此人的ID在1列中重复5次,每个人在另一列中配对并回答问题
如下所示:</ p>

  ID答案
1 A1
1 A4
1 A2
1 A9
1 A3
12 A1
12 A11
12 A12
12 A17
12 A2
</ code> </ pre>

我想要尝试做的是合并所有 答案为1个数组,其ID为
something </ p>

  array(
[1] =&gt; array(0 =&gt;'A1',1 =&gt;') A4',2 =&gt;'A2',3 =&gt;'A9',4 =&gt;'A3'),
[12] =&gt;阵列(0 =&gt;'A1',1 =&gt; 'A11',2 =&gt;'A12',3 =&gt;'A17',4 =&gt;'A2')

</ code> </ pre>

我的代码 如下:</ p>

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

}
</ code> </ pre>

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

展开原文

原文

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:

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

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.

4个回答



只需更改 foreach </ code>循环,即可根据需要构建生成的数组。</ p>
\ n

  foreach($ quiz_answers as $ aa){
$ ss [$ aa ['response_id']] [] = $ aa ['answer'];
}
</ code> < / pre>

根据需要提供 $ ss </ code>数组:</ p>

  array(
1 =&gt; array) ('A1','A4','A2','A9','A3'),
12 =&gt;数组('A1','A11','A12','A17','A2') ,

</ code> </ pre>
</ div>

展开原文

原文

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

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

This gives an $ss array as you want:

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

duanran3115
duanran3115 比我的解决方案好多了,它更简洁。 请改用它。
7 年多之前 回复



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

  array(
[1] =&gt; array(0 =&gt;'A1',0 =&gt;'A4',0 =&gt;'A2 ',0 =&gt;'A9',0 =&gt;'A3'),
[12] =&gt;数组(0 =&gt;'A1',0 =&gt;'A11',0 =&gt;' A12',0 =&gt;'A17',0 =&gt;'A2')

</ code> </ pre>

所以这样的东西可以工作(下面的骨架代码) </ p>

  $ answers = array(); 
foreach($ quiz_answers as $ id =&gt; $ ans){
//检查$ answers [$ id]是否存在, 否则在这里创建....
if(exists(....))...
else $ answers [$ id] = array();

//然后添加当前的答案 ,如下所示
array_push($ answers [$ id],$ ans);
}
</ code> </ pre>
</ div>

展开原文

原文

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.

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

duan1983
duan1983 array_push($ answers [$ aa ['response_id']],array($ aa ['answer'])); 返回以下错误array_push()期望参数1为数组,给定null
7 年多之前 回复
dtzhfb4869
dtzhfb4869 甚至我使用array_push而不是array_merge。
7 年多之前 回复



  echo“&lt; pre /&gt;”; 
for($ i = 0; $ i&lt; count($ array) ); $ i ++)
{
$ output [$ array [$ i] ['ID']] [] = $ array [$ i] ['Answer'];
}
nprint_r($ output);

</ code> </ pre>
</ div>

展开原文

原文

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



  $ quiz_answers = //从数据库中获取所有数据。
$ res = array();
foreach($ quiz_answers为$ key =&gt; $ val){
$ res [$ val ['response_id']] [] = $ val ['answer'];
}

print_r($ res);
< / code> </ pre>
</ div>

展开原文

原文

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

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐