How can I loop through two arrays at once?(如何一次循环遍历两个数组?)
问题描述
我正在尝试将两个数组排列在一起,但结果总是不正确.我将向您展示我的代码、我得到的结果以及我正在寻找的结果.
I am trying to arrange two arrays together and the results keep coming out incorrect. I'll show you my code, the results I'm getting and the results I'm looking for.
我想我只是做错了,但不知道该怎么做.
I guess I'm just doing it wrong but not sure how else to do it.
我的代码:
$data1 = [
'a: 1',
'a: 2',
'a: 3',
'a: 4',
'a: 5'
];
$data2 = [
'b: 1',
'b: 2',
'b: 3',
'b: 4',
'b: 5'
];
foreach($data1 as $item1)
{
foreach($data2 as $item2)
{
echo $item1 . '<br/>';
echo $item2 . '<br/>';
echo '<br/><br/>';
}
}
结果:(缩短以节省空间)
The Results: (shortened to save space)
a: 1
b: 1
a: 1
b: 2
a: 1
b: 3
a: 1
b: 4
a: 1
b: 5
我正在寻找的结果如下:
The results I'm looking for is the following:
a: 1
b: 1
a: 2
b: 2
a: 3
b: 3
a: 4
b: 4
a: 5
b: 5
推荐答案
问题
问题当然是您的嵌套 foreach 循环.因为对于您的 $data1
数组的每个元素,您都会遍历整个 $data2
数组(所以总共有 $data1
* $data2
次迭代).
Problem
Well the problem is of course your nested foreach loop. Because for each element of your $data1
array you loop through the entire $data2
array (So in total there are $data1
* $data2
iterations).
要解决这个问题,您必须同时遍历两个数组.
To solve this you have to loop through both arrays at once.
您可以使用 array_map()
并将所有要同时循环的数组传递给它.
You can do this with array_map()
and pass all arrays to it which you want to loop through at the same time.
array_map(function($v1, $v2){
echo $v1 . "<br>";
echo $v2 . "<br><br>";
}, $data1, $data2 /* , Add more arrays if needed manually */);
注意:如果元素的数量不均匀,你不会产生任何错误,它只会打印 NULL
(意味着你不会看到任何东西)
Note: If the amount of elements are uneven you won't gen any errors, it will just print NULL
(Means you won't see anything)
使用 MultipleIterator
和 附加 ArrayIterator
根据需要.
Use a MultipleIterator
and attach as many ArrayIterator
as you need.
$it = new MultipleIterator();
$it->attachIterator(new ArrayIterator($data1));
$it->attachIterator(new ArrayIterator($data2));
//Add more arrays if needed
foreach($it as $a) {
echo $a[0] . "<br>";
echo $a[1] . "<br><br>";
}
注意:如果元素的数量不均匀,它将只打印两个数组仍有值的所有值
Note: If the amount of elements are uneven it will just print all values where both arrays still have values
使用带有计数器变量的 for 循环,您可以将其用作两个数组的键.
Use a for loop with a counter variable, which you can use as key for both arrays.
$keysOne = array_keys($data1);
$keysTwo = array_keys($data2);
$min = min(count($data1), count($data2));
for($i = 0; $i < $min; $i++) {
echo $data1[$keysOne[$i]] . "<br>";
echo $data2[$keysTwo[$i]] . "<br><br>";
}
注意:使用array_keys()
只是这样,如果数组没有相同的键或关联,这也可以工作.min()
仅用于循环遍历每个数组都有很多元素
或者如果数组只有唯一值,你可以 array_combine()
两个数组,因此 $data1
可以作为键访问,$data2
作为值访问.
Or if the arrays only have unique values, you can array_combine()
both arrays, so that $data1
can be accessed as key and $data2
as value.
foreach(array_combine($data1, $data2) as $d1 => $d2) {
echo $d1 . "<br>";
echo $d2 . "<br><br>";
}
注意:数组必须有相同数量的元素,否则array_combine()
会抛出错误
Note: The arrays must have the same amount of elements, otherwise array_combine()
will throw an error
如果您想同时打印超过 2 个数组或只打印未知数量的数组,您可以将 array_map()
方法与 call_user_func_array()
调用.
If you want to print more than 2 arrays at the same time or just an unknown amount of arrays, you can combine the array_map()
method with a call_user_func_array()
call.
$func = function(...$numbers){
foreach($numbers as $v)
echo $v . "<br>";
echo "<br>";
};
call_user_func_array("array_map", [$func, $data1, $data2]);
注意:由于这有点使用 array_map()
方法,它的行为与不均匀数量的元素相同.此外,由于此方法仅适用于 PHP >=5.6,您只需删除参数并使用 $numbers.func-get-args.php" rel="noreferrer">func_get_args()
在 foreach 循环中,然后它也适用于 PHP >=5.3
Note: Since this kinda uses the array_map()
method, it behaves the same with uneven amount of elements. Also since this method only works for PHP >=5.6 you can just remove the arguments and change out $numbers
with func_get_args()
in the foreach loop and then it also works for PHP >=5.3
这篇关于如何一次循环遍历两个数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何一次循环遍历两个数组?
基础教程推荐
- 在多维数组中查找最大值 2021-01-01
- 在 PHP 中强制下载文件 - 在 Joomla 框架内 2022-01-01
- 在 Woocommerce 中根据运输方式和付款方式添加费用 2021-01-01
- WooCommerce 中选定产品类别的自定义产品价格后缀 2021-01-01
- Libpuzzle 索引数百万张图片? 2022-01-01
- XAMPP 服务器不加载 CSS 文件 2022-01-01
- mysqli_insert_id 是否有可能在高流量应用程序中返回 2021-01-01
- 如何在 PHP 中的请求之间持久化对象 2022-01-01
- 超薄框架REST服务两次获得输出 2022-01-01
- 通过 PHP SoapClient 请求发送原始 XML 2021-01-01