Website Development Prices

Search Blog

Sunday, March 6, 2016

Prolaz kroz visedimenzionalni niz (Looping through a multidimensional array)

Ako imate niz sa dve dimenzije, prvo promenite prvi indeks, a onda drugi u unutrasnjoj petlji. Ovo mozete uraditi pomocu ugnjezdenih petlji.

Primer:

<?php

$cene_motora[0] [] = 2500;
$cene_motora[0] [] = 2350;
$cene_motora[1] [] = 3700;
$cene_motora[1] [] = 3550;

for ($spoljni_indeks = 0; 
$spoljni_indeks < count($cene_motora);
$spoljni_indeks++) {
for ($unutrasnji_indeks = 0; 
$unutrasnji_indeks < count($cene_motora[$spoljni_indeks]);
$unutrasnji_indeks++) {
echo "\$cene_motora[$spoljni_indeks][$unutrasnji_indeks] = ", 
$cene_motora[$spoljni_indeks][$unutrasnji_indeks], "<br />";
}

}

?>


Sacuvajte ovaj fajl kao prolaz-kroz-visedimenzionalni-niz.php.

Objasnjenje: na ovaj nacin mozete da podesite prvi indeks niza, odstampate sve elemente promenom drugog indeksa, a da onda povecate drugi indeks. 

Rezultat:

$cene_motora[0][0] = 2500
$cene_motora[0][1] = 2350
$cene_motora[1][0] = 3700
$cene_motora[1][1] = 3550


Mozete da koristite petlje tipa foreach. Ovo petlje je bolje koristiti, ako koristite nizove sa tekstualnim indeksima, Napomena: ovi indeksi se ne mogu povecavati u obicnoj petlji. 

Primer 2: u svakom prolazu kroz spoljasnju petlju vadi se po jedan jednodimenzionalni niz, kroz koji se zatim prolazi. Kroz unutrasnji niz prolazimo pomocu foreach petlje.

<?php

$cene_motora["suzuki"] ["prva_cena"] = 2500;
$cene_motora["suzuki"] ["druga_cena"] = 2350;
$cene_motora["honda"] ["prva_cena"] = 3700;
$cene_motora["honda"] ["druga_cena"] = 3550;

foreach 
($cene_motora as $spoljni_kljuc => $jednodimenzionalni_niz) {
foreach ($jednodimenzionalni_niz as $unutrasnji_kljuc => $vrednost) {
echo "\$cene_motora[$spoljni_kljuc][$unutrasnji_kljuc] = $vrednost<br />";
}

}

?>

Rezultat:

$cene_motora[suzuki][prva_cena] = 2500
$cene_motora[suzuki][druga_cena] = 2350
$cene_motora[honda][prva_cena] = 3700
$cene_motora[honda][druga_cena] = 3550


If you have an array with two dimensions, first, change the first index, and then other in the inner loop. You can do this by using nested loops.

Example:

<?php

$motorcycle_prices[0] [] = 2500;
$motorcycle_prices[0] [] = 2350;
$motorcycle_prices[1] [] = 3700;
$motorcycle_prices[1] [] = 3550;

for ($outer_index = 0; 
$outer_index < count($motorcycle_prices);
$outer_index++) {
for ($inner_index = 0; 
$inner_index < count($motorcycle_prices[$outer_index]);
$inner_index++) {
echo "\$motorcycle_prices[$outer_index][$inner_index] = ", 
$motorcycle_prices[$outer_index][$inner_index], "<br />";
}

}

?>

Save this file as a looping-through-a-multidimensional-array.php.

Explanation: this way you can set the first index of array, print out all elements changing the second index, and then increase the second index.

Result:

$motorcycle_prices[0][0] = 2500
$motorcycle_prices[0][1] = 2350
$motorcycle_prices[1][0] = 3700
$motorcycle_prices[1][1] = 3550


You can use the loop type foreach. This loop is better to use, if you use arrays with text indexes. Note: these indexes can not be increased in an ordinary loop.

Example 2: in each loop through the outer loop is taken out one of one-dimensional array, through which you can loop. Through inner array we can loop using foreach loop.

<?php

$motorcycle_prices["suzuki"] ["first_price"] = 2500;
$motorcycle_prices["suzuki"] ["second_price"] = 2350;
$motorcycle_prices["honda"] ["first_price"] = 3700;
$motorcycle_prices["honda"] ["second_price"] = 3550;

foreach 
($motorcycle_prices as $outer_key => $one_dimensional_array) {
foreach ($one_dimensional_array as $inner_key => $value) {
echo "\$motorcycle_prices[$outer_key][$inner_key] = $value<br />";
}

}

?>  

Result:

$motorcycle_prices[suzuki][first_price] = 2500
$motorcycle_prices[suzuki][second_price] = 2350
$motorcycle_prices[honda][first_price] = 3700

$motorcycle_prices[honda][second_price] = 3550

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.