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

发布于 2024-09-08 10:08:25 字数 983 浏览 2 评论 0原文

         $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;

我创建数组 $data 的方式正确吗?如果是的话...现在如果我想将数组早餐传递给一个函数,我该如何获取其中的数组早餐?

我想做这样的事情:

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

那么我现在如何从 $data 中获取早餐数组呢?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

大姐,你呐 2024-09-15 10:08:25

如果您希望 $breakfast$lunch$dinner$data 中创建多维数组,您需要将您的作业替换为以下内容。

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

然后您可以访问其中任何一个,例如 $data['breakfast']['value']

我不太明白DB部分。回复评论,我很乐意提供帮助。

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.

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

Then you can access any of them like $data['breakfast']['value'].

I didn't quite understand the DB part. Comment back and I'd be glad to help.

余生一个溪 2024-09-15 10:08:25

正确的是

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

. 运算符表示字符串连接。您可以使用 $data[0] 访问 $breakfast,因为它是第一个成员。您还可以使用关联数组;请参阅@Jason 的回答。

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. You can also use associative arrays; see @Jason's answer.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文