简体   繁体   中英

PHP Multiple arrays in foreach loop

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)

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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM