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)
3
  • 1
    why foreach if only 1 item in arrays? Commented Jun 29, 2015 at 10:48
  • possible duplicate of Two arrays in foreach loop Commented Jun 29, 2015 at 10:48
  • Because there is going to be more, I just made one test query. Commented Jun 29, 2015 at 10:49

1 Answer 1

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
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.