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

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

报告相同问题?