说我有这3个阵列:
Product(milk,candy,chocolate)
Colors(white,red,black)
Rating(8,7,9)
如何创建一个循环来绑定这些数组,以便在每个循环中得到3个变量:$product
$color
$rating
因此,通过示例,我将输出如下:
牛奶 白色且评分 8 / 10
糖果 红色且评分 7 / 10
巧克力 黑色且评分 9 / 10
由于
答案 0 :(得分:8)
E.g。使用SPL的MultipleIterator
<?php
$Product=array('milk','candy','chocolate');
$Colors=array('white','red','black');
$Rating=array(8,7,9);
$it = new MultipleIterator;
$it->attachIterator(new ArrayIterator($Product));
$it->attachIterator(new ArrayIterator($Colors));
$it->attachIterator(new ArrayIterator($Rating));
foreach( $it as $e ) {
echo join(' - ', $e), "\n";
}
打印
milk - white - 8
candy - red - 7
chocolate - black - 9
答案 1 :(得分:3)
我不知道我是否做对了。 但是你想要这样的东西吗?
$products = array("milk", "candy", "chocolate");
$colors = array("white", "red", "black");
$ratings = array(8, 7, 9);
for($i = 0; $i < sizeof($products); $i++) {
$product = $products[$i];
$color = $colors[$i];
$rating = $ratings[$i];
echo $product . ' is ' . $color . ' and has a rating of ' . $rating . '/10 <br/>';
}
输出将是:
milk is white and has the rating of 8/10
candy is red and has the rating of 7/10
chocolate is black and has the rating of 9/10