在php中使用多维数组的array_merge

using this foreach loop :

foreach($course_presence as $key => $value){
    $presence_arr[$value['student']] = array();
    array_push($presence_arr[$value['student']],$value); 
}

i create array that its keys are the students id's . student id = $value['student'].(1,2,3...)

$value has the following structure exp: array(3) { ["student"]=> string(1) "1" ["course"]=> string(4) "1111" ["date"]=> string(19) "2016-03-17 10:25:00" }

why if this loop executes 10 times for two different ids i dont get all the arrays for each student id?

EXAMPLE: in my case currently the loop iterates over this data:

array(3) { ["student"]=> string(1) "1" ["course"]=> string(4) "1111" ["date"]=> string(19) "2016-03-17 10:25:00" }
 array(3) { ["student"]=> string(1) "5" ["course"]=> string(4) "1111" ["date"]=> string(19) "2016-01-01 10:00:00" }
 array(3) { ["student"]=> string(1) "5" ["course"]=> string(4) "1111" ["date"]=> string(19) "2016-01-22 00:00:00" } 
array(3) { ["student"]=> string(1) "5" ["course"]=> string(4) "1111" ["date"]=> string(19) "2016-02-14 00:00:00" }
 array(3) { ["student"]=> string(1) "5" ["course"]=> string(4) "1111" ["date"]=> string(19) "2016-02-28 00:00:00" }
 array(3) { ["student"]=> string(1) "1" ["course"]=> string(4) "1111" ["date"]=> string(19) "2016-02-14 14:00:00" }
 array(3) { ["student"]=> string(1) "5" ["course"]=> string(4) "1111" ["date"]=> string(19) "2016-03-27 00:00:00" }

and var_dump of the presence_arr is this:

    array(2) { 
[1]=> array(1) { [0]=> array(3) { ["student"]=> string(1) "1" ["course"]=> string(4) "1111" ["date"]=> string(19) "2016-02-14 14:00:00" } }
 [5]=> array(1) { [0]=> array(3) { ["student"]=> string(1) "5" ["course"]=> string(4) "1111" ["date"]=> string(19) "2016-01-03 05:00:00" } } }

as you cans the merge ran over and replaced the value in each index instead of merging? ani idea what causing that? thx

doukekui0914
doukekui0914 你是对的......对我来说很愚蠢......
4 年多之前 回复
dpprx26000
dpprx26000 我认为你的问题出现在$presence_arr[$value['student']]=array();线。$presence_arr[$value['student']]被分配给一个新的空数组。如果$presence_arr[$value['student']]中已有任何数据,则将其删除并替换为空数组,以防止发生任何合并。
4 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐