dongwang6837 2016-04-23 13:39
浏览 131

在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

  • 写回答

0条回答

    报告相同问题?

    悬赏问题

    • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
    • ¥15 找一位技术过硬的游戏pj程序员
    • ¥15 matlab生成电测深三层曲线模型代码
    • ¥50 随机森林与房贷信用风险模型
    • ¥50 buildozer打包kivy app失败
    • ¥30 在vs2022里运行python代码
    • ¥15 不同尺寸货物如何寻找合适的包装箱型谱
    • ¥15 求解 yolo算法问题
    • ¥15 虚拟机打包apk出现错误
    • ¥15 用visual studi code完成html页面