38

Is it possible to have a foreach loop in PHP with multiple "index" variables, akin to the following (which doesn't use correct syntax)?

foreach ($courses as $course, $sections as $section)

If not, is there a good way to achieve the same result?

Donald T
  • 10,234
  • 17
  • 63
  • 91
  • You might want to clarify. Are you trying to loop through a 2D array or 2 arrays? – Jere Nov 11 '10 at 18:21
  • 1
    Not sure how this would work? Can you explain a bit more what your doing? You could have a nested foreach or combine the array before iteration. Let us know what you're trying to do and we can help you do it. – Andy Groff Nov 11 '10 at 18:21

8 Answers8

56

to achieve just that result you could do

foreach (array_combine($courses, $sections) as $course => $section)

but that only works for two arrays

Will
  • 4,498
  • 1
  • 21
  • 20
  • 4
    Note; if `$courses` are numerical strings, they will turned into integers. – hakre Feb 15 '12 at 19:36
  • Hopefully I'm not ressurcting a thread that is too dead. But, I'm trying to achieve the same thing, I had for a second, but I dind't think it worked, but it didn, but for the life of me can't remember the code I put in. I'm trying to have category and value go into the database. foreach($_POST['category'] as $i => $category) { // Get values from post. $category = mysql_real_escape_string($category); $value = mysql_real_escape_string($_POST['value'][$i]); – Nomad Apr 23 '14 at 21:08
  • what if both array have different value – TarangP Nov 02 '17 at 08:58
18

If both the arrays are of the same size you can use a for loop as:

for($i=0, $count = count($courses);$i<$count;$i++) {
 $course  = $courses[$i];
 $section = $sections[$i];
}
RiaD
  • 46,822
  • 11
  • 79
  • 123
codaddict
  • 445,704
  • 82
  • 492
  • 529
9

TRY -

1)

<?php
$FirstArray = array('a', 'b', 'c', 'd');
$SecondArray = array('1', '2', '3', '4');

foreach($FirstArray as $index => $value) {
    echo $FirstArray[$index].$SecondArray[$index];
    echo "<br/>";
}
?>

or 2)

<?php
$FirstArray = array('a', 'b', 'c', 'd');
$SecondArray = array('1', '2', '3', '4');

for ($index = 0 ; $index < count($FirstArray); $index ++) {
  echo $FirstArray[$index] . $SecondArray[$index];
  echo "<br/>";
}
?>
T.Todua
  • 53,146
  • 19
  • 236
  • 237
8

You would need to use nested loops like this:

foreach($courses as $course)
{
    foreach($sections as $section)
    {
    }
}

Of course, this will loop over every section for every course.

If you want to look at each pair, you are better off using either objects that contain the course/section pairs and looping over those, or making sure the indexes are the same and doing:

foreach($courses as $key => $course)
{
    $section = $sections[$key];
}
Alan Geleynse
  • 24,821
  • 5
  • 46
  • 55
  • I've used both the accepted answer and the `$courses as $key => $course` answer above successfully within Wordpress. The difference is that the accepted answer is limited to two custom fields whereas this answer allows any number of fields and is therefore more flexible. – edwinbradford Oct 05 '18 at 13:59
4

No, because those arrays may have other number of items.

You must explicitely write something like that:

for ($i = 0; $i < count($courses) && $i < count($sections); ++$i) {
    $course = $courses[$i];
    $section = $sections[$i];

    //here the code you wanted before
}
Daimon
  • 3,703
  • 2
  • 28
  • 30
  • 1
    pre-execute your '$i < count(...' code and store it in a variable so that it's not executed on every iteration of the for loop. For large arrays this is significant savings. – zzzzBov Nov 11 '10 at 18:25
  • 1
    I work with very large projects which work under high load and usually it really doesn't matter because single SQL query is 1000 slower than php count :). And if you have to work with VERY large arrays then there is something wrong with application design. – Daimon Nov 11 '10 at 18:28
  • Using Count inside the parameters will result in a very expensive transaction. – Samuel Ramzan Apr 11 '21 at 08:40
3

No, this is maybe one of the rare cases PHP's array cursors are useful:

reset($sections);
foreach ($courses as $course)
{
 list($section) = each($sections);
}
AndreKR
  • 32,613
  • 18
  • 106
  • 168
2

What would that do, exactly? Are $courses and $sections just two separate arrays, and you want to perform the same function for the values in each? You could always do:

foreach(array_merge($courses, $sections) as $thing) { ... }

This makes all the usual assumptions about array_merge, of course.

Or is it that $sections comes from $course and you want to do something for each section in each course?

foreach($courses as $course) {
    foreach($sections as $section) {
        // Here ya go
    }
}
VoteyDisciple
  • 37,319
  • 5
  • 97
  • 97
0

like this?

foreach($array as $b=>$c){

}
Edmhs
  • 3,645
  • 27
  • 39