【问题标题】:PHP store multi-dimensional array's key in foreachPHP在foreach中存储多维数组键
【发布时间】:2023-03-16 21:35:01
【问题描述】:

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

我知道如何使用 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次,怎么办?感谢您的帮助。

【问题讨论】:

  • 感谢 Verhaeren。是 foreach($BookingInfo[$roomNumber][$id] as $bookDate => $array) 在第三个 foreach 吗?谢谢。
  • Verhaeren,我已经更新了数组。谢谢。
  • @Verhaeren,使用 foreach($value as $id => $val){ 有什么问题?它比您的建议更容易阅读。并且 2 是等效的,前提是您只从数组中读取。如果您担心对数组的修改,那么通过引用分配会更简洁,即将第一个 foreach 更改为 foreach($BookingInfo as $roomNumber => &$value) {,但我认为这不是这个特定用例的要求。

标签: php arrays multidimensional-array foreach


【解决方案1】:

在最里面的第二个循环中将 bookDate 添加到详细信息数组中可能是最简单的:

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";
            }
        }
    }                   
}

(只要确保您使用的任何键都不是可能已经在 details 数组中的键,否则您可能会造成一些混乱)。

输出见http://codepad.org/oQT3cmo8

【讨论】:

  • 谢谢卢卡斯,但我有一个问题,为什么使用 var_dump($detail["bookDate"]) 结果会重复 5 次?另外我需要将数组收集到函数中,但它对每个值执行 5 次......我该如何解决它,或者我应该在 foreach 之外调用函数?非常感谢你,卢卡斯。
  • 你把var_dump($detail["bookDate"])放在哪里?如果它在最里面的循环中,那么它将为每个细节打印一次。
猜你喜欢
  • 2014-07-21
  • 2013-11-27
  • 2017-10-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-01
  • 1970-01-01
相关资源
最近更新 更多