Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing two variables into a 'foreach' loop

I need help regarding a foreach() loop. aCan I pass two variables into one foreach loop?

For example,

foreach($specs as $name, $material as $mat)
{
    echo $name;
    echo $mat;
}

Here, $specs and $material are nothing but an array in which I am storing some specification and material name and want to print them one by one. I am getting the following error after running:

Parse error: syntax error, unexpected ',', expecting ')' on foreach line.

like image 478
Manish Mishra Avatar asked Oct 05 '15 05:10

Manish Mishra


1 Answers

In the Beginning, was the For Loop:

$n = sizeof($name);
for ($i=0; i < $n; $i++) {
   echo $name[$i];
   echo $mat[$i];
}
like image 135
paulsm4 Avatar answered Sep 22 '22 06:09

paulsm4