简体   繁体   English

在php中创建和使用多维数组

[英]Creating and using multi-dimensional array in php

         $breakfast = array(
                               'rest_id' => $rest_id  ,
                               'type' => 'Breakfast' ,
                               'value' => $bprice  
                                );



            $lunch = array(
                               'rest_id' => $rest_id  ,
                               'type' => 'Facilities' ,
                               'value' => $lprice  
                                );


            $dinner = array(
                               'rest_id' => $rest_id  ,
                               'type' => 'Facilities' ,
                               'value' => $dprice  
                                );

            $data = $breakfast . $lunch . $dinner;

Is the way i created the array $data correct? 我创建数组$ data的方式正确吗? If yes... now how do i get the array breakfast inside it, if i want to pass it to one function? 如果是的话...现在,如果我想将其传递给一个函数,该如何在其中获取数组早餐?

I want to do something like this: 我想做这样的事情:

$this->db->insert_breakfast($breakfast);

So how do i get breakfast array out of $data now? 那么,现在如何从$ data中获取早餐数组呢?

If you want $breakfast , $lunch and $dinner to make a multi-dimensional array in $data , you will need to replace your assignment with the following. 如果您希望$breakfast$lunch$dinner$data创建一个多维数组,则需要用以下内容替换您的分配。

$data = array('breakfast' => $breakfast, 'lunch' => $lunch, 'dinner' => $dinner);

Then you can access any of them like $data['breakfast']['value'] . 然后,您可以访问任何它们,例如$data['breakfast']['value']

I didn't quite understand the DB part. 我不太了解数据库部分。 Comment back and I'd be glad to help. 发表评论,我们很乐意为您提供帮助。

The correct is 正确的是

$data = array($breakfast, $lunch, $dinner);

The . . operator means string concatenation. 运算符表示字符串连接。 You can access $breakfast with $data[0] , since it's the first member. 您可以使用$breakfast $data[0]来访问$breakfast ,因为它是第一个成员。 You can also use associative arrays; 您也可以使用关联数组。 see @Jason's answer. 参见@Jason的答案。

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

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