Jump to content

foreach for 2 arrays


asmith

Recommended Posts

on this forum , i remember i wanted to solve some other guys question , but my solution needed to loop 2 arrays at the same time,so i couldn't answer him .

now i was just wondering if i want to loop more than 1, maybe more than 3 arrays at the same time , but would be the easiest way ?

 

hmm

 

$arr1 = array(2 => 34,6 => 23,12 => 76);
$arr2 = array(54 = > 2,14 => 97,2 => 44);
$arr3 = array(8=> 3,67 => 21,26 => 34);

 

hmmm each array value multiply on its key + the same with second + the same with third,  add all final numbers together.

 

i'm typing write now.. i mean your question made me show that example, maybe the solution is easy .  how YOU do it with loops ?

 

 

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/85419-foreach-for-2-arrays/#findComment-435824
Share on other sites

you could use each()

 

$arr1 = array(2 => 34,6 => 23,12 => 76);
$arr2 = array(54 = > 2,14 => 97,2 => 44);
$arr3 = array(8=> 3,67 => 21,26 => 34);
while(1){
  list($key1,$val1) = each($arr1);
  list($key2,$val2) = each($arr2);
  list($key3,$val3) = each($arr3);
  if(!$key1 || !$key2 || !$key3) break;

  ...do work here...
}

Link to comment
https://forums.phpfreaks.com/topic/85419-foreach-for-2-arrays/#findComment-435831
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.