Nasıl diziler bağlamak için?

2 Cevap php

I bu 3 diziler var ki:

Product(milk,candy,chocolate)
Colors(white,red,black)
Rating(8,7,9)

I her döngüde 3 değişkenleri olsun bu diziler bağlamak için bir döngü oluşturmak için: $product $color $rating

Yani örnek ben böyle çıktısı:

Milk olup white ve 8 / 10 bir derece vardır

Candy olup red ve 7 / 10 bir derece vardır

Chocolate olup black ve 9 / 10 bir derece vardır

Teşekkürler

2 Cevap

I dont know if I get it right. But do you want something like this?

$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/>';
 }

Çıktı olacaktır:

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 

Örneğin SPL en MultipleIterator ile

<?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";
}

baskılar

milk - white - 8
candy - red - 7
chocolate - black - 9