douyudouchao6779 2014-12-19 02:29
浏览 46
已采纳

PHP在foreach中存储多维数组键

It's an interesting situation for getting the array key in multi-dimensional array.

I know how to get the array value by using foreach but how to get the key value and insert into database??

Here is my code:

   //Array
   $BookingInfo = array(
            "115"=>array(
                "date"=>array(
                    "15/12/2014"=>array(//need to get the date but not in here
                        array(
                            //need to get the date in here!!
                            "from"=>2,
                            "to"=>5,
                            "user"=>"Ella",
                            "userid"=>"b2111"
                                ),
                        array(
                            "from"=>5,
                            "to"=>7,
                            "user"=>"Johnson",
                            "userid"=>"a2413"
                                )   
                        ),
                    "16/12/2014"=>array(
                        array(
                            "from"=>4,
                            "to"=>8,
                            "user"=>"Peter",
                            "userid"=>"g531"
                                )
                        ),
                     "17/12/2014"=>array(
                        array(
                            "from"=>1,
                            "to"=>3,
                            "user"=>"Chris",
                            "userid"=>"h024"
                                ),
                        array(
                            "from"=>3,
                            "to"=>6,
                            "user"=>"Jennifer",
                            "userid"=>"f314"
                                )
                        ),
                    "20/12/2014"=>array(
                        array(
                            "from"=>1,
                            "to"=>5,
                            "user"=>"Raymond",
                            "username"=>"r362"
                                )
                        ),
                    "21/12/2014"=>array(
                        array(
                            "from"=>1,
                            "to"=>6,
                            "user"=>"Amy",
                            "username"=>"a754"
                                )
                        ),
                    "23/08/2014"=>array(
                        array(
                            "from"=>2,
                            "to"=>4,
                            "user"=>"Amy",
                            "userid"=>"m432"
                                )
                        )
                    )
                )
        );

The foreach code:

    foreach($BookingInfo as $roomNumber => $value){
        foreach($value as $id => $val){
            foreach($val as $bookDate => $array){
                foreach($array as $key => $detail){
                    foreach($detail as $period =>$info){
                        //get the $bookDate here
                        //if I get the "$bookDate" here, it shows the result with repeating 3 times, how can I solve it??   
                    }
                }
            }
        }                   
    }

And I want to get the "15/12/2014" 2 times because of two members' booking, and the "16/12/2014" 1 times, what is the method to do it? Thanks for help.

  • 写回答

1条回答 默认 最新

  • doubian6241 2014-12-19 02:56
    关注

    It's probably easiest to just add the bookDate to the detail array, in the second innermost loop:

    foreach($BookingInfo as $roomNumber => $value){
        foreach($value as $id => $val){
            foreach($val as $bookDate => $array){
                foreach($array as $key => $detail){
                    $detail['bookDate'] = $bookDate;
                    foreach($detail as $detailkey =>$detailval){
                        print "$detailkey => $detailval
    "; 
                    }
                    print "***
    ";
                }
            }
        }                   
    }
    

    (Just make sure that whatever key you use isn't one that might be in the details array already or you might cause some confusion).

    See http://codepad.org/oQT3cmo8 for output

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?