Multiple index variables in PHP foreach loop
Solution 1
to achieve just that result you could do
foreach (array_combine($courses, $sections) as $course => $section)
but that only works for two arrays
Solution 2
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];
}
Solution 3
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/>";
}
?>
Solution 4
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];
}
Solution 5
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
}
Donald T
A software engineer who develops cutting-edge Web applications.
Updated on March 28, 2020Comments
-
Donald T about 4 years
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?
-
zzzzBov over 13 yearspre-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.
-
Daimon over 13 yearsI 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.
-
hakre about 12 yearsNote; if
$courses
are numerical strings, they will turned into integers. -
Nomad about 10 yearsHopefully 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]);
-
TarangP over 6 yearswhat if both array have different value
-
edwinbradford over 5 yearsI'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. -
Samuel Ramzan about 3 yearsUsing Count inside the parameters will result in a very expensive transaction.