在PHP的foreach循环中合并三个数组
问题描述:
我知道如何使用PHP的 array_combine()
函数在foreach循环中合并两个数组
I know how to combine two arrays in foreach loop using array_combine()
function of PHP
但是我有三个数组,我想一次遍历所有三个数组.
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/>";
}
我也想在此循环中迭代 $ get_comment
数组.
I want to iterate $get_comment
array too in this loop.
谢谢
答
我认为这可能是您想要的:
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/>";
}
此解决方案假定$ get_id,$ get_product和$ get_comment数组都以相同的方式建立索引.
This solution assumes the $get_id, $get_product, and $get_comment arrays are all indexed the same way.