boucles - Comment fonctionne PHP 39;foreachdent#39

Mots clés : phploopsforeachiterationphp-internalsphp

meilleur 5 Réponses boucles - Comment fonctionne PHP 39;foreachdent#39

vote vote

97

foreach ($it as $k => $v) { /* ... */ }  /* translates to: */  if ($it instanceof IteratorAggregate) {     $it = $it->getIterator(); } for ($it->rewind(); $it->valid(); $it->next()) {     $v = $it->current();     $k = $it->key();     /* ... */ } 
// Using by-ref iteration here to make sure that it's really // the same array in both loops and not a copy foreach ($arr as &$v1) {     foreach ($arr as &$v) {         // ...     } } 
function iterate($arr) {     foreach ($arr as $v) {} }  $outerArr = [0, 1, 2, 3, 4]; iterate($outerArr); 
reset(arr); while (get_current_data(arr, &data) == SUCCESS) {     code();     move_forward(arr); } 
reset(arr); while (get_current_data(arr, &data) == SUCCESS) {     move_forward(arr);     code(); } 
foreach ($array as $val) {     var_dump(current($array)); } /* Output: 2 2 2 2 2 */ 
$ref = &$array; foreach ($array as $val) {     var_dump(current($array)); } /* Output: 2 3 4 5 false */ 
foreach ($array as &$val) {     var_dump(current($array)); } /* Output: 2 3 4 5 false */ 
$foo = $array; foreach ($array as $val) {     var_dump(current($array)); } /* Output: 1 1 1 1 1 */ 
foreach ($array as &$v1) {     foreach ($array as &$v2) {         if ($v1 == 1 && $v2 == 1) {             unset($array[1]);         }         echo "($v1, $v2)\n";     } }  // Output: (1, 1) (1, 3) (1, 4) (1, 5) 
$array = [1, 2, 3, 4, 5]; foreach ($array as &$value) {     var_dump($value);     reset($array); } // output: 1, 2, 3, 4, 5 
$array = [1, 2, 3, 4, 5]; $ref =& $array; foreach ($array as $value) {     var_dump($value);     unset($array[1]);     reset($array); } // output: 1, 1, 3, 4, 5 
$array = ['EzEz' => 1, 'EzFY' => 2, 'FYEz' => 3]; $ref =& $array; foreach ($array as $value) {     unset($array['EzFY']);     $array['FYFY'] = 4;     reset($array);     var_dump($value); } // output: 1, 4 
$arr = [1, 2, 3, 4, 5]; $obj = (object) [6, 7, 8, 9, 10];  $ref =& $arr; foreach ($ref as $val) {     echo "$val\n";     if ($val == 3) {         $ref = $obj;     } } /* Output: 1 2 3 6 7 8 9 10 */ 
$array = [1, 2, 3, 4, 5]; $ref = &$array; foreach ($array as $val) {     var_dump($val);     $array[2] = 0; } /* Old output: 1, 2, 0, 4, 5 */ /* New output: 1, 2, 3, 4, 5 */ 
$obj = new stdClass; $obj->foo = 1; $obj->bar = 2; foreach ($obj as $val) {     var_dump($val);     $obj->bar = 42; } /* Old and new output: 1, 42 */ 
$array = [1, 2, 3, 4, 5]; foreach ($array as &$v1) {     foreach ($array as &$v2) {         if ($v1 == 1 && $v2 == 1) {             unset($array[1]);         }         echo "($v1, $v2)\n";     } }  // Old output: (1, 1) (1, 3) (1, 4) (1, 5) // New output: (1, 1) (1, 3) (1, 4) (1, 5) //             (3, 1) (3, 3) (3, 4) (3, 5) //             (4, 1) (4, 3) (4, 4) (4, 5) //             (5, 1) (5, 3) (5, 4) (5, 5)  
$array = ['EzEz' => 1, 'EzFY' => 2, 'FYEz' => 3]; foreach ($array as &$value) {     unset($array['EzFY']);     $array['FYFY'] = 4;     var_dump($value); } // Old output: 1, 4 // New output: 1, 3, 4 
vote vote

80

$a = array(1,2,3); $b = $a;  // This is lazy cloning of $a. For the time           // being $a and $b point to the same internal           // data structure.  $a[] = 3; // Here $a changes, which triggers the actual           // cloning. From now on, $a and $b are two           // different data structures. The same would           // happen if there were a change in $b. 
vote vote

72

$array = array(1, 2, 3, 4, 5);  list($key2, $val2) = each($array); echo "each() Original (outside): $key2 => $val2<br/>";  foreach($array as $key => $val){     echo "foreach: $key => $val<br/>";      list($key2,$val2) = each($array);     echo "each() Original(inside): $key2 => $val2<br/>";      echo "--------Iteration--------<br/>";     if ($key == 3){         echo "Resetting original array pointer<br/>";         reset($array);     } }  list($key2, $val2) = each($array); echo "each() Original (outside): $key2 => $val2<br/>"; 
each() Original (outside): 0 => 1 foreach: 0 => 1 each() Original(inside): 1 => 2 --------Iteration-------- foreach: 1 => 2 each() Original(inside): 2 => 3 --------Iteration-------- foreach: 2 => 3 each() Original(inside): 3 => 4 --------Iteration-------- foreach: 3 => 4 each() Original(inside): 4 => 5 --------Iteration-------- Resetting original array pointer foreach: 4 => 5 each() Original(inside): 0=>1 --------Iteration-------- each() Original (outside): 1 => 2 
vote vote

67

<?     $array = Array(         'foo' => 1,         'bar' => 2     );     foreach($array as $k=>&$v) {         $array['baz']=3;         echo $v." ";     }     print_r($array); ?> 
1 2 3 Array (     [foo] => 1     [bar] => 2     [baz] => 3 ) 
<?     $array = Array(         'foo' => 1,         'bar' => 2     );     foreach($array as $k=>&$v) {         if ($k=='bar') {             $array['baz']=3;         }         echo $v." ";     }     print_r($array); ?> 
1 2 Array (     [foo] => 1     [bar] => 2     [baz] => 3 ) 
vote vote

57

$array = ['foo'=>1]; foreach($array as $k=>&$v) {    $array['bar']=2;    echo($v); } 
$array = ['foo'=>1, 'bar'=>2]; foreach($array as $k=>&$v) {    $array['baz']=3;    echo($v); } 

Questions similaires