2

我有一个名为 emp_rec 的数组,有 100 多名员工,每个员工有大约 60 个字段,我使用以下方法一次使用一名员工......

foreach($emp_rec as $obj) {
   $name = $obj->get_empname();
  //.....
  ......///
}

现在计划在一个循环中一次使用三个员工,我该怎么做...?

4

4 回答 4

1

你可以试试这个:

$current = Array();
while(($current[0] = array_shift($emp_rec))
   && ($current[1] = array_shift($emp_rec))
   && ($current[2] = array_shift($emp_rec))) {
  // do stuff here
}
if( $current[0]) {
    // there were records left over, optionally do something with them.
}
于 2013-01-11T08:30:17.973 回答
0

尝试这样的事情:

for ($i = 0; $i < count($emp_rec); $i+=3) {
    $emp1 = $emp_rec[$i];
    $emp2 = $emp_rec[$i+1];
    $emp3 = $emp_rec[$i+2];
}
于 2013-01-11T08:29:31.857 回答
0

Here you can iterate in one time on same multi objects. Easy to adapt.

<?php
// Example of class
class A {
    public $a = 'a';
    public $b = 'b';
    public $c = 'c';
}

$obj1 = new A; // Instantiate 3 objects
$obj2 = new A;
$obj3 = new A;

$objs = array((array)$obj1, (array)$obj2, (array)$obj3); // Array of objects (cast in array)

foreach ($objs[0] as $key => $value) {
    echo $objs[0][$key];
    echo $objs[1][$key];
    echo $objs[2][$key];
}

Output aaabbbccc

于 2013-01-11T08:40:54.317 回答
0

关于什么 :

$GROUP_SIZE = 3;

$emp_count = count($emp_rec);
for ($i=0; $i<$emp_count; $i+=$GROUP_SIZE) {
    for ($j=0; $i+$j<$emp_count && $j<$GROUP_SIZE; $j++) {
        $current = $emp_rec[$i+$j];
        $name = $current->get_empname();
    }
}

如果您需要一次操纵 3 或 N 名员工,它会让您知道当前员工在哪个“组”中。

于 2013-01-11T10:19:35.583 回答