繁体   English   中英

PHP将多维数组的键存储在foreach中

[英]PHP store multi-dimensional array's key in 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”的奖励,而要两次获得“ 16/12/2014”的奖励,请问该怎么做? 感谢帮助。

在最里面的第二个循环中,仅将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";
            }
        }
    }                   
}

(只需确保您使用的密钥不是detail数组中已经存在的密钥,否则可能会引起混淆)。

有关输出,请参见http://codepad.org/oQT3cmo8

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM