there is an array

array(7){ [0]=> array(7) { ["id"]=> string(2) "13" ["id_user"]=> string(1) "2" ["id_serial"]=> string(1) "6" ["date"]=> string(10) "2016-06-26" ["active"]=> string(1) "1" ["total_count"]=> string(1) "4" } [1]=> array(7) { ["id"]=> string(2) "17" ["id_user"]=> string(1) "2" ["id_serial"]=> string(1) "5" ["date"]=> string(10) "2016-06-26" ["active"]=> string(1) "1" ["total_count"]=> string(1) "2" } } 

it comes in the $ tday variable to the $ top variable onboarding doesn't have a code

 $count=0; foreach($top as $post){ тут код foreach ($tday[$count] as $today) { var_dump($today)."<br/>"; } и дальше код $count=$count+1; } 

What is the point I need to get an array that corresponds to the iteration number of the foreach ($ top as $ post) cycle and continue to work with it, but I get it

  string(2) "13" string(1) "2" string(1) "6" string(10) "2016-06-26" string(1) "1" string(1) "4" string(2) "13" string(1) "2" string(1) "6" string(10) "2016-06-26" string(1) "1" string(1) "4" 

I would still like to get at the first iteration

 [0]=> array(7) { ["id"]=> string(2) "13" ["id_user"]=> string(1) "2" ["id_serial"]=> string(1) "6" ["date"]=> string(10) "2016-06-26" ["active"]=> string(1) "1" ["total_count"]=> string(1) "4" } 

and at the second

 [0]=> array(7) { ["id"]=> string(2) "17" ["id_user"]=> string(1) "2" ["id_serial"]=> string(1) "5" ["date"]=> string(10) "2016-06-26" ["active"]=> string(1) "1" ["total_count"]=> string(1) "2" } 

so that I could work with them for example to make a condition

 $count=0; foreach($top as $post){ foreach ($tday[$count] as $today) { echo $today['id']."<br/>"; $count=$count+1;} } 

and to get

 13 17 

considering that there will be ten arrays

  • Unclear. Want to get an array with the same keys? - Alexey Shimansky
  • Or does he want Print_r .... - Denis Kotlyarov
  • @ Maybe I didn’t put it right I need to get two arrays, but so that they would remain arrays - Sergalas
  • @Sergalas purely display? Well, echo '<pre>'; print_r($today); echo '</pre>'; echo '<pre>'; print_r($today); echo '</pre>'; ..... instead of a print you can wardamp - Alexey Shimansky
  • @ AlekseyShimansky, I made a change. - Sergalas

2 answers 2

The foreach loop is used to iterate over arrays. $ count is not needed. At each iteration you already get the next array to work with.

 $arr = array( [0] => array( ["id"] => "13", ["id_user"] => "2", ["id_serial"] => "6", ["date"] => "2016-06-26", ["active"] => "1", ["total_count"] => "4" ), [1] => array( ["id"] => "17", ["id_user"] => "2", ["id_serial"] => "5", ["date"] => "2016-06-26", ["active"] => "1", ["total_count"] => "2" ) ); foreach($arr as $key => $value) { echo $value['id'].' '.$value['date']; } 
  • I changed it a bit so that you would understand why I was driving a meter - Sergalas

this is how $ count = 0;

 foreach($top as $post){ тут код $today = $tday[$count]; echo $today['id']."<br/>"; и дальше код $count=$count+1; }