Diziler ve foreach Kullanımı
foreach kullanmadan yapılan dizi işlemlerinin zorluğunu anlatan bir örnek
<?php
$markalar[0] = "BMW";
$markalar[1] = "VW";
$markalar[2] = "FIAT";
$markalar[3] = "RENAULT";
$markalar = array("BMW", "VW", "FIAT", "RENAULT");
$modeller = array(0 => array("3 serisi", "5 serisi", "7 serisi"));
$markalar = array(0 => "BMW", 1 => "VW", 2 => "FIAT", 3 => "RENAULT");
$modeller = array(
0 => array(0 => "3 serisi", 1 => "5 serisi", 2 => "7 serisi"),
1 => array(0 => "passat", 1 => "golf")
);
for ($i = 0; $i < count($markalar); $i++) {
echo $markalar[$i] . "<br>";
for ($j = 0; $j < count($modeller[$i]); $j++) {
echo $modeller[$i][$j] . ", ";
}
echo "<hr>";
}
$araclar = array("BMW" => array("3 serisi", "5 serisi", "7 serisi"), "VW" => array("passat", "golf", "bora"));
foreach ($araclar as $indis => $deger) {
echo $indis . "<br>";
foreach ($deger as $deg) {
echo $deg;
}
echo "<hr>";
}
?>
$markalar[0] = "BMW";
$markalar[1] = "VW";
$markalar[2] = "FIAT";
$markalar[3] = "RENAULT";
$markalar = array("BMW", "VW", "FIAT", "RENAULT");
$modeller = array(0 => array("3 serisi", "5 serisi", "7 serisi"));
$markalar = array(0 => "BMW", 1 => "VW", 2 => "FIAT", 3 => "RENAULT");
$modeller = array(
0 => array(0 => "3 serisi", 1 => "5 serisi", 2 => "7 serisi"),
1 => array(0 => "passat", 1 => "golf")
);
for ($i = 0; $i < count($markalar); $i++) {
echo $markalar[$i] . "<br>";
for ($j = 0; $j < count($modeller[$i]); $j++) {
echo $modeller[$i][$j] . ", ";
}
echo "<hr>";
}
$araclar = array("BMW" => array("3 serisi", "5 serisi", "7 serisi"), "VW" => array("passat", "golf", "bora"));
foreach ($araclar as $indis => $deger) {
echo $indis . "<br>";
foreach ($deger as $deg) {
echo $deg;
}
echo "<hr>";
}
?>
Yorumunuzu Ekleyin