php - How do two foreach function work if 1 foreach is inside the other -


i cant understand logic of how 2 foreach functions work if 1 of them inside other, example this:

foreach ($projects $project){     foreach ($users $user){      } } 

when first foreach gets project, foreach of users goes thru users till has none , returns , repeat? or when finds first user too?

this might seem pretty basic question not being able find somewhere else.

you can try yourself, quite simple. write 2 dummy-arrays , iterate on them 2 loops. below example.

  1. it find project.
  2. the inner foreach user-loop executed each user object found
  3. it find next project
  4. it again run inner foreach-loop project.

what want that:

<?php  $array1 = array(     "element1" => "value1",     "element2" => "value2",     "element3" => "value3" );  $array2 = array(     "element1.1" => "value1.1",     "element2.1" => "value2.1",     "element3.1" => "value3.1" );  foreach($array1 $arr1) {     print_r($arr1);     echo "<br>";      foreach($array2 $arr2) {         print_r($arr2);         echo "<br>";     } } 

output:

value1 value1.1 value2.1 value3.1 value2 value1.1 value2.1 value3.1 value3 value1.1 value2.1 value3.1 

Comments

Popular posts from this blog

neo4j - finding mutual friends in a cypher statement starting with three or more persons -

php - How to remove letter in front of the word laravel -

minify - Minimizing css files -