php 如何在“单个”foreach() 循环中使用多个数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10326346/
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
How to use multiple arrays in " single " foreach() loop
提问by yusufiqbalpk
I'm trying to echo out all variables of two arrays with a single foreach loop making a url. Some coding examples will be honored.
我正在尝试使用一个 foreach 循环来回显两个数组的所有变量,从而生成一个 url。一些编码示例将受到尊重。
I have this so far :
到目前为止我有这个:
<?php
foreach($menu_names as $menu_name){
echo "<li><a href='?subj= " .urlencode($subjects["id"]). " '>".$menu_name."</a></li>";
}
?>
I want to add one more array within this loop
我想在这个循环中再添加一个数组
回答by C. Leung
assume you have the same number of items in those two arrays.
假设您在这两个数组中有相同数量的项目。
if you want to use foreach(), then the arrays need to have the same index:
如果要使用foreach(),则数组需要具有相同的索引:
foreach ($a as $index => $value)
{
echo $a[$index] .' '. $b[$index];
}
if the arrays have numeric index, you can just use for():
如果数组具有数字索引,则可以使用for():
for ($i = 0; $i < sizeof($a); $i++)
{
echo $a[$i] .' '. $b[$i];
}
回答by dhoom
array_combine()will merge your 2 arrays into one with key=>valuepairs and then access them through foreach statement
e.g
array_combine()将您的 2 个数组成key=>value对合并,然后通过 foreach 语句访问它们,例如
foreach(array_combine($a,$b) as $key=>$value) {
echo $key."<br/>".$value;
}
回答by cHao
The answer depends on what exactlyyou need to do with the arrays and how you need to do it.
答案取决于您究竟需要对数组做什么以及您需要如何做。
If your arrays have similar indexes, and you need to use the same element from each array, you could do like
如果您的数组具有相似的索引,并且您需要使用每个数组中的相同元素,您可以这样做
foreach ($array1 as $index => $value1) {
$value2 = $array2[$index];
// do stuff with $value1 and $value2 here
}
(Although in this case, particularly if you find yourself doing this a lot, you may want to think about using a single array of either objects or arrays, so that the data will always be together and easier to connect.)
(尽管在这种情况下,特别是如果您发现自己经常这样做,您可能需要考虑使用对象或数组的单个数组,以便数据始终在一起并且更容易连接。)
Or, if the arrays have the same type of elements in it and you want to iterate over both in order, you could iterate over array_merge($array1, $array2). (If the arrays aren't numerically indexed, though, and particularly if they have the same stringy keys, one of the arrays' elements could replace the other. See the docs on array_mergefor details.)
或者,如果数组中包含相同类型的元素,并且您想按顺序迭代两者,则可以迭代array_merge($array1, $array2). (不过,如果数组没有数字索引,特别是如果它们具有相同的字符串键,则数组的元素之一可以替换另一个。有关详细信息,请参阅文档array_merge。)
There are a bunch of other possibilities, depending on how you need the elements. (The question really doesn't provide any info on that.)
还有很多其他的可能性,这取决于您如何需要这些元素。(这个问题确实没有提供任何信息。)
回答by T.Todua
Full Code:
完整代码:
1)
1)
<?php
$First = array('a', 'b', 'c', 'd');
$Second = array('1', '2', '3', '4');
foreach($First as $indx => $value) {
echo $First[$indx].$Second[$indx];
echo "<br />";
}
?>
or 2)
或 2)
<?php
$First = array('a', 'b', 'c', 'd');
$Second = array('1', '2', '3', '4');
for ($indx = 0 ; $indx < count($First); $indx ++) {
echo $First[$indx] . $Second[$indx];
echo "<br />";
}
?>

