如何将一个数组元素分配给另一个数组?

时间:2017-11-22 06:37:56

标签: php arrays codeigniter-3

我在数据库中有两个表,一个表是 saloon_staff ,第二个表是 saloon_staff_timings

Saloon_staff enter image description here

saloon_staff_timings enter image description here

现在在 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>";
     } 
?>

此代码的结果。 enter image description here

第一个foreach循环显示沙龙工作人员的名字,第二个foreach循环显示沙龙工作人员的时间。我在第二个foreach循环中遇到问题,我想得到的是名称和时间对应该名称。但是现在,我在数据库中可以获得所有时间,这是错误的。

Saloon工作人员时间print_r($ saloon_staff)

  Array
    (
        [0] => stdClass Object
            (
                [staff_name] => Salman Iqbal
                [id] => 1
            )

        [1] => stdClass Object
            (
                [staff_name] => Alludin 
                [id] => 2
            )

    )

Saloon工作人员时间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 
            )

    )

任何帮助将不胜感激。

4 个答案:

答案 0 :(得分:2)

简单如果声明你就错过了......试试这个: -

<?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答案

答案 1 :(得分:2)

只需使用INNER JOIN连接两个表,并按使用条件

显示数据相应的列
SELECT ss.*,sst.* FROM `Saloon_staff` INNER JOIN `Saloon_Staff_timings` AS sst ON sst.`saloon_staff_id`= ss.`saloon_profile_id`

答案 2 :(得分:2)

您可以尝试使用以下代码段。它应该工作。

<?php 

  foreach ($saloon_staff as $key => $value) {

  echo"<tr>";
  echo"<td> $value->staff_name </td>";

  foreach ($staff_timings as $key => $stf_timngs) {

     if($value->id  == $stf_timngs->saloon_staff_id ){
          echo  "<td>".$stf_timngs->start_time."</td>";
      }

    } 

   echo"</tr>";

 } 

在您的代码中,您只是打印出裁剪而不管用户的时间安排。所以我只是在第一个foreach循环中添加了一个针对用户ID的检查,并在时间表中添加了人员配置文件ID。这应该导致与特定用户相关的修剪。

答案 3 :(得分:1)

您需要检查$saloon_staff id$staff_timings ID的ID。之所以打印全部是因为我们没有检查它

以下是代码:

foreach ($saloon_staff as $key => $value){
    echo '<tr>';
    echo '<td>' . $value->staff_name . '</td>';
    foreach ($staff_timings as $key2 => $stf_timngs) {
        // Check ID if equal
        if($value->id == $stf_timings->saloon_staff_id){
            echo '<td>' . $stf_timngs->start_time . '</td>';
        }
    } 
    echo"</tr>";
} 

我也清理代码。这更容易阅读,因为我在echo中包含html标签而不是分隔html。