Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to assign one array elements to another array?

I have two tables in database one table is saloon_staff and the second table is saloon_staff_timings

Saloon_staff enter image description here

saloon_staff_timings enter image description here

Now in the saloon_staff_timings table, I am inserting the timing of saloon staff. I have run two queries to get data from the table, The first query is getting the names of saloon staff through saloon_staff_id in saloon_staff_timings table and the second query is getting the timings of the saloon staff.

Here is my code.

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

The result of this code. enter image description here

The first foreach loop is showing the names of saloon staff and the second foreach loop is showing the timings of the saloon staff. I have a problem in the second foreach loop, what I want to get is the name and the time against that name. But for now, I am getting all the times available in the database, which is wrong.

Saloon staff timings print_r($saloon_staff)

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

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

    )

Saloon staff timings 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] => [email protected]
                [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] => [email protected]
                [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] => [email protected]
                [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] => [email protected]
                [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] => [email protected]
                [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] => [email protected]
                [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] => [email protected]
                [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] => [email protected]
                [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] => [email protected]
                [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] => [email protected]
                [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] => [email protected]
                [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] => [email protected]
                [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] => [email protected]
                [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] => [email protected]
                [appointments_enabled] => 1
                [role_in_saloon] => No Access
                [name] => Alludin 
            )

    )

Any help will be appreciated.

like image 500
Salman Iqbal Avatar asked Nov 18 '25 16:11

Salman Iqbal


1 Answers

Simple You are missing if statement...Try this:-

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

Hope it helps!.. if you want to do with joins refer the @sajeevan answer

like image 200
kunal Avatar answered Nov 21 '25 07:11

kunal



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!