php – 如何将一个数组元素分配给另一个数组?
作者:互联网
我在数据库中有两个表,一个表是saloon_staff,第二个表是saloon_staff_timings
现在在saloon_staff_timings表中,我正在插入沙龙工作人员的时间.我已经运行了两个查询来从表中获取数据,第一个查询是通过saloon_staff_timings表中的saloon_staff_id获取沙龙工作人员的姓名,第二个查询是获取沙龙工作人员的时间.
这是我的代码.
<?php
foreach ($saloon_staff as $key => $value)
{
echo"<tr>";
echo"<td> $value->staff_name </td>";
foreach ($staff_timings as $key => $stf_timngs)
{
?>
<td><?php echo $stf_timngs->start_time ?></td>
<?php
}
echo"</tr>";
}
?>
第一个foreach循环显示沙龙工作人员的名字,第二个foreach循环显示沙龙工作人员的时间.我在第二个foreach循环中遇到问题,我想得到的是名称和时间对应该名称.但是现在,我在数据库中可以获得所有时间,这是错误的.
沙龙员工时间print_r($saloon_staff)
Array
(
[0] => stdClass Object
(
[staff_name] => Salman Iqbal
[id] => 1
)
[1] => stdClass Object
(
[staff_name] => Alludin
[id] => 2
)
)
沙龙员工时间print_r($staff_timings)
Array
(
[0] => stdClass Object
(
[id] => 1
[saloon_staff_id] => 1
[day] => Monday
[start_time] => 01:03:00
[end_time] => 18:00:00
[break_start] => 00:00:00
[break_end] => 00:00:00
[saloon_profiles_id] => 1
[phone_number] => 098098098
[email] => SalmanIq@facebook.com
[appointments_enabled] => 1
[role_in_saloon] => Owner
[name] => Salman Iqbal
)
[1] => stdClass Object
(
[id] => 1
[saloon_staff_id] => 1
[day] => Tuesday
[start_time] => 03:06:00
[end_time] => 19:04:00
[break_start] => 00:00:00
[break_end] => 00:00:00
[saloon_profiles_id] => 1
[phone_number] => 098098098
[email] => SalmanIq@facebook.com
[appointments_enabled] => 1
[role_in_saloon] => Owner
[name] => Salman Iqbal
)
[2] => stdClass Object
(
[id] => 1
[saloon_staff_id] => 1
[day] => Wednesday
[start_time] => 02:08:00
[end_time] => 21:08:00
[break_start] => 00:00:00
[break_end] => 00:00:00
[saloon_profiles_id] => 1
[phone_number] => 098098098
[email] => SalmanIq@facebook.com
[appointments_enabled] => 1
[role_in_saloon] => Owner
[name] => Salman Iqbal
)
[3] => stdClass Object
(
[id] => 1
[saloon_staff_id] => 1
[day] => Thursday
[start_time] => 01:00:00
[end_time] => 14:08:00
[break_start] => 00:00:00
[break_end] => 00:00:00
[saloon_profiles_id] => 1
[phone_number] => 098098098
[email] => SalmanIq@facebook.com
[appointments_enabled] => 1
[role_in_saloon] => Owner
[name] => Salman Iqbal
)
[4] => stdClass Object
(
[id] => 1
[saloon_staff_id] => 1
[day] => Friday
[start_time] => 01:06:00
[end_time] => 19:09:00
[break_start] => 00:00:00
[break_end] => 00:00:00
[saloon_profiles_id] => 1
[phone_number] => 098098098
[email] => SalmanIq@facebook.com
[appointments_enabled] => 1
[role_in_saloon] => Owner
[name] => Salman Iqbal
)
[5] => stdClass Object
(
[id] => 1
[saloon_staff_id] => 1
[day] => Saturday
[start_time] => 01:09:00
[end_time] => 15:09:00
[break_start] => 00:00:00
[break_end] => 00:00:00
[saloon_profiles_id] => 1
[phone_number] => 098098098
[email] => SalmanIq@facebook.com
[appointments_enabled] => 1
[role_in_saloon] => Owner
[name] => Salman Iqbal
)
[6] => stdClass Object
(
[id] => 1
[saloon_staff_id] => 1
[day] => Sunday
[start_time] => 00:03:00
[end_time] => 19:09:00
[break_start] => 00:00:00
[break_end] => 00:00:00
[saloon_profiles_id] => 1
[phone_number] => 098098098
[email] => SalmanIq@facebook.com
[appointments_enabled] => 1
[role_in_saloon] => Owner
[name] => Salman Iqbal
)
[7] => stdClass Object
(
[id] => 2
[saloon_staff_id] => 2
[day] => Monday
[start_time] => 02:03:00
[end_time] => 16:09:00
[break_start] => 00:00:00
[break_end] => 00:00:00
[saloon_profiles_id] => 1
[phone_number] => 98790809809
[email] => alludin@gmail.com
[appointments_enabled] => 1
[role_in_saloon] => No Access
[name] => Alludin
)
[8] => stdClass Object
(
[id] => 2
[saloon_staff_id] => 2
[day] => Tuesday
[start_time] => 01:09:00
[end_time] => 20:01:00
[break_start] => 00:00:00
[break_end] => 00:00:00
[saloon_profiles_id] => 1
[phone_number] => 98790809809
[email] => alludin@gmail.com
[appointments_enabled] => 1
[role_in_saloon] => No Access
[name] => Alludin
)
[9] => stdClass Object
(
[id] => 2
[saloon_staff_id] => 2
[day] => Wednesday
[start_time] => 01:09:00
[end_time] => 21:00:00
[break_start] => 00:00:00
[break_end] => 00:00:00
[saloon_profiles_id] => 1
[phone_number] => 98790809809
[email] => alludin@gmail.com
[appointments_enabled] => 1
[role_in_saloon] => No Access
[name] => Alludin
)
[10] => stdClass Object
(
[id] => 2
[saloon_staff_id] => 2
[day] => Thursday
[start_time] => 11:00:00
[end_time] => 13:09:00
[break_start] => 00:00:00
[break_end] => 00:00:00
[saloon_profiles_id] => 1
[phone_number] => 98790809809
[email] => alludin@gmail.com
[appointments_enabled] => 1
[role_in_saloon] => No Access
[name] => Alludin
)
[11] => stdClass Object
(
[id] => 2
[saloon_staff_id] => 2
[day] => Friday
[start_time] => 01:07:00
[end_time] => 21:00:00
[break_start] => 00:00:00
[break_end] => 00:00:00
[saloon_profiles_id] => 1
[phone_number] => 98790809809
[email] => alludin@gmail.com
[appointments_enabled] => 1
[role_in_saloon] => No Access
[name] => Alludin
)
[12] => stdClass Object
(
[id] => 2
[saloon_staff_id] => 2
[day] => Saturday
[start_time] => 00:00:00
[end_time] => 00:00:00
[break_start] => 00:00:00
[break_end] => 00:00:00
[saloon_profiles_id] => 1
[phone_number] => 98790809809
[email] => alludin@gmail.com
[appointments_enabled] => 1
[role_in_saloon] => No Access
[name] => Alludin
)
[13] => stdClass Object
(
[id] => 2
[saloon_staff_id] => 2
[day] => Sunday
[start_time] => 00:00:00
[end_time] => 00:00:00
[break_start] => 00:00:00
[break_end] => 00:00:00
[saloon_profiles_id] => 1
[phone_number] => 98790809809
[email] => alludin@gmail.com
[appointments_enabled] => 1
[role_in_saloon] => No Access
[name] => Alludin
)
)
任何帮助将不胜感激.
解决方法:
简单如果声明你就错过了…试试这个: –
<?php
foreach ($saloon_staff as $key => $value){
echo"<tr>";
echo"<td> $value->staff_name </td>";
foreach ($staff_timings as $key => $stf_timngs){
if($stf_timngs->saloon_staff_id == $value->id ){?>
<td><?php echo $stf_timngs->start_time ?></td>
<?php } }
echo"</tr>";
} ?>
希望它有所帮助!..如果您想使用连接,请参阅@sajeevan答案
标签:codeigniter-3,php,arrays 来源: https://codeday.me/bug/20190727/1550777.html