I know how to combine two arrays in foreach loop using array_combine()
function of PHP
我知道如何使用PHP的array_combine()函数在foreach循环中组合两个数组
But I have three arrays and I want to loop through all of three arrays at a time.
但我有三个数组,我想一次遍历所有三个数组。
$get_id=$data->get_id;
$get_product=$data->get_product;
$get_comment=$data->get_comment;
foreach (array_combine($get_id, $get_product) as $id => $product) {
echo "$id - $product<br/>";
}
I want to iterate $get_comment
array too in this loop.
我想在这个循环中迭代$ get_comment数组。
Thanks
谢谢
2 个解决方案
#1
1
I think this might be what you are looking for:
我想这可能就是你要找的东西:
$get_id=$data->get_id;
$get_product=$data->get_product;
$get_comment=$data->get_comment;
foreach($get_id as $i => $id){
$product = $get_product[$i];
$comment = $get_comment[$i];
echo "$id , $product, $comment<br/>";
}
This solution assumes the $get_id, $get_product, and $get_comment arrays are all indexed the same way.
此解决方案假定$ get_id,$ get_product和$ get_comment数组都以相同的方式编制索引。
#2
0
Combine the arrays before the foreach loop
在foreach循环之前组合数组
$comment_array = array_combine($get_id, $get_comment);
$product_array = array_combine($get_id, $get_product);
foreach ($product_array as $id => $product) {
$comment = $comment_array[$id];
}
#1
1
I think this might be what you are looking for:
我想这可能就是你要找的东西:
$get_id=$data->get_id;
$get_product=$data->get_product;
$get_comment=$data->get_comment;
foreach($get_id as $i => $id){
$product = $get_product[$i];
$comment = $get_comment[$i];
echo "$id , $product, $comment<br/>";
}
This solution assumes the $get_id, $get_product, and $get_comment arrays are all indexed the same way.
此解决方案假定$ get_id,$ get_product和$ get_comment数组都以相同的方式编制索引。
#2
0
Combine the arrays before the foreach loop
在foreach循环之前组合数组
$comment_array = array_combine($get_id, $get_comment);
$product_array = array_combine($get_id, $get_product);
foreach ($product_array as $id => $product) {
$comment = $comment_array[$id];
}