[英]associating two separate arrays for itinerary system
I am trying to build a travel itinerary system. 我正在尝试建立旅行路线系统。 The user selects the dates of travel, and then may add items to each day.
用户选择旅行日期,然后可以向每天添加项目。
I have an array of dates, stored in a session in the format: 我有一个日期数组,以以下格式存储在会话中:
array(
(int) 0 => '2012-08-25',
(int) 1 => '2012-08-26',
(int) 2 => '2012-08-27'
)
They will then choose attractions, which I wish to store in an array in the format: 然后,他们将选择景点,我希望这些景点以以下格式存储在数组中:
array(
(int) 0 => array(
'Attraction' => array(
'attraction_id' =>'1',
'name' => 'Place One',
)
),
(int) 1 => array(
'Attraction' => array(
'attraction_id' => '2',
'name' => 'Place Two',
)
),
I'd like to be able to output: 我希望能够输出:
So, each item of the first array contains an array of items, if that makes sense. 因此,第一个数组的每个项目都包含一个项目数组,如果这有意义的话。 I am struggling with the logic of associating the keys of the
days
array with the items
array. 我正在努力将
days
数组的键与items
数组关联。
I looked at array_merge
but that doesn't seem to do what I need. 我看着
array_merge
但这似乎并没有满足我的需要。
Is there an easy way to achieve this? 有没有简单的方法可以做到这一点?
This code does exactly what you ask. 此代码完全符合您的要求。 Unfortunately, I fear your question doesn't reflect your aim given the example.
不幸的是,鉴于这个例子,我担心您的问题不能反映您的目标。 Using keys to link data will led to 1-1 relationship, where as you seem to need a 1-n.
使用键链接数据将导致1-1关系,而您似乎需要1-n。 You should have a foreign key field in the attraction array, like date_id.
您应该在吸引力数组中有一个外键字段,例如date_id。
$array= array();
foreach($dates as $date_key=>$date){
$array[$date]=array();
foreach($attractions as $attraction_key=>$attraction){
if($date_key==$attraction_key){
$array[$date][]=$attraction['Attraction']['name'];
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.