0

I need to somehow get two arrays in one foreach loop (Because two loops would mess it up). The two arrays is selected from my database. As you can see in the var_dump down below, then there are the same values, I do not know is this is going to be a problem?

Code:

$getUserWorks       = $work->getUserWorks($userInfo->id);
$getUserMachines    = $work->getUserMachines($userInfo->id);

foreach(???){
?>
                            <tr>
                                <td>
                                    <?php echo $array->orderid; ?>
                                </td>
                                <td>
                                     <?php echo $array->work_hours; ?>
                                </td>
                                <td>
                                    <?php 
                                    echo $array->machinename;
                                    ?>
                                </td>
                                <td>
                                    <?php echo $array->machine_hours; ?>
                                </td>
                                <td>

                                </td>
                            </tr>
                            </tbody>
<?php } ?>

Var_dump of the two arrays:

array (size=1)
  0 => 
    object(stdClass)[11]
      public 'id' => string '2' (length=1)
      public 'orderid' => string '4' (length=1)
      public 'userid' => string '8' (length=1)
      public 'work_hours' => string '4' (length=1)
      public 'timestamp' => string '2015-06-28 12:48:34' (length=19)

array (size=1)
  0 => 
    object(stdClass)[7]
      public 'id' => string '2' (length=1)
      public 'orderid' => string '4' (length=1)
      public 'userid' => string '8' (length=1)
      public 'workid' => string '2' (length=1)
      public 'machineid' => string '1' (length=1)
      public 'machinename' => string 'test' (length=4)
      public 'machine_hours' => string '4' (length=1)
      public 'timestamp' => string '2015-06-28 12:48:34' (length=19)

1 Answers1

2

Using SPL's MultipleIterator

$mi = new MultipleIterator();
$mi->attachIterator(new ArrayIterator($getUserWorks));
$mi->attachIterator(new ArrayIterator($getUserMachines));
$result = array();
foreach($mi as list($userWork, $userMachine)) {
    .... do stuff here
}
Mark Baker
  • 209,507
  • 32
  • 346
  • 385