PHP在foreach中存储多维数组键

时间:2014-12-19 02:29:46

标签: php arrays multidimensional-array foreach

在多维数组中获取数组键是一个有趣的情况。

我知道如何使用foreach获取数组值,但是如何获取键值并插入数据库?

这是我的代码:

   //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"
                                )
                        )
                    )
                )
        );

foreach代码:

    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??   
                    }
                }
            }
        }                   
    }

我希望得到" 15/12 / 2014" 2次因为两名成员'预订," 16/12/2014" 1次,这样做的方法是什么?谢谢你的帮助。

1 个答案:

答案 0 :(得分:1)

在第二个最里面的循环中,将bookDate添加到detail数组可能最容易:

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\n"; 
                }
                print "***\n";
            }
        }
    }                   
}

(只需确保您使用的任何密钥都不是可能位于详细信息数组中的密钥,否则您可能会造成一些混淆)。

请参阅http://codepad.org/oQT3cmo8了解输出