PHP foreach 循环中的多个索引变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4157810/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 12:08:23  来源:igfitidea点击:

Multiple index variables in PHP foreach loop

phploopsforeachfor-loop

提问by Donald Taylor

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

是否可以foreach在 PHP 中使用多个“索引”变量进行循环,类似于以下内容(不使用正确的语法)?

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

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

如果没有,是否有达到相同结果的好方法?

回答by Will

to achieve just that result you could do

为了达到你可以做的结果

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

but that only works for two arrays

但这仅适用于两个数组

回答by codaddict

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

如果两个数组的大小相同,您可以使用for循环作为:

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

回答by Alan Geleynse

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];
}

回答by T.Todua

TRY -

尝试 -

1)

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)

或 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/>";
}
?>

回答by Daimon

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
}

回答by AndreKR

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

不,这可能是 PHP 的数组游标有用的罕见情况之一:

reset($sections);
foreach ($courses as $course)
{
 list($section) = each($sections);
}

回答by VoteyDisciple

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

那会做什么呢?是$courses$sections刚才两个独立的阵列,并且要执行相同的功能在每个值是多少?你总是可以这样做:

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

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

array_merge当然,这做出了关于 的所有通常假设。

Or is it that $sectionscomes from $courseand you want to do something for each section in each course?

或者它$sections来自$course并且您想为每个课程的每个部分做一些事情?

foreach($courses as $course) {
    foreach($sections as $section) {
        // Here ya go
    }
}

回答by Edmhs

like this?

像这样?

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

}