0

我如何跟踪第一个 foreach 循环并将其标记为 1?我使用了模数,但有时会导致两个结果为 $i == 1。我希望第一个循环的每个循环都有一个带有 1 的结果。

这是我的代码。

$array  = array(
                array('val'  =>   1), //Mark this as $i = 1;
                array('val'  =>   1), //Mark this as $i = 0;
                array('val'  =>   2), //Mark this as $i = 1;
                array('val'  =>   2), //Mark this as $i = 0;
                array('val'  =>   3)  //Mark this as $i = 1;
                );

foreach($array as $key1=>$arr1) {

        foreach($array as $key2=>$arr2) {

               $i = ($key1 % 2 == 0) ? 1 : 0;

               //Output expected
               // 1 vs 1
               // 2 vs 2
               // 3

               break;

        }

}

希望你能明白。

4

1 回答 1

0

您不能将相同的名称放在两个数组$array中,请更改

foreach($array as $key1=>$arr1) {

        foreach($array as $key2=>$arr2) {

   foreach($array as $key1=>$arr1) {

            foreach($array123 as $key2=>$arr2) {
于 2013-10-10T15:50:51.367 回答