Kroz niz mozete da prodjete pomocu funkcije count(), koja odredjuje koliko se elemenata nalazi u nizu.
Primer:
<?php
$motor[0] = "suzuki";
$motor[1] = "ktm";
$motor[2] = "honda";
for ($indeks = 0; $indeks < count($motor); $indeks++) {
echo $motor[$indeks], "<br />";
}
?>
Rezultat:
suzuki
ktm
honda
Funkcija koja prikazuje element niza je funkcija print_r().
Primer 2:
<?php
$motor[0] = "suzuki";
$motor[1] = "ktm";
$motor[2] = "honda";
print_r($motor);
?>
Rezultat:
Array ( [0] => suzuki [1] => ktm [2] => honda )
Za prolaz kroz kolekcije kao sto su nizovi koristite iskaz foreach. Ovaj iskaz ima dva oblika:
foreach (izraz_niza as $vrednost) iskaz
Ovaj oblik dodeljuje promenljivoj $vrednost vrednost sledeceg elementa iz niza pri svakom prolazu kroz petlju.
foreach (izraz_niza as $kljuc => $vrednost) iskaz
Ovaj oblik postavlja kljuc tekuceg elementa (tj. indeks), u promenljivu $kljuc i to radi za svaki prolaz kroz petlju.
Primer 3: prikaz svih elemenata niza pomocu iskaza foreach.
<?php
$motor = array("suzuki", "ktm", "honda");
foreach ($motor as $vrednost) {
echo "Vrednost: $vrednost<br />";
}
?>
Rezultat:
Vrednost: suzuki
Vrednost: ktm
Vrednost: honda
Primer 4: prikaz i kljuceva i vrednosti niza.
<?php
$motor = array("suzuki", "ktm", "honda");
foreach ($motor as $kljuc => $vrednost) {
echo "Kljuc: $kljuc; Vrednost: $vrednost<br />";
}
?>
Rezultat:
Kljuc: 0; Vrednost: suzuki
Kljuc: 1; Vrednost: ktm
Kljuc: 2; Vrednost: honda
Pomocu petlje while takodje mozete da prodjete kroz niz, sa funkcijom each(). Ova funkcija se koristi za prolaz kroz kolekcije (tj. nizove). Funkcija each us vakom prolazu vraca kljuc tekuceg elementa , a onda prelazi na sledeci element. Ako zelite da rukujete visestrukim vrednostima koje se dobijaju iz niza, mozete da koristite funkciju list(). Ova funkcija dve povratne vrednosti dodeljuje posebnim promenljivama.
Primer 5:
<?php
$motor = array("suzuki", "ktm", "honda");
while (list($kljuc, $vrednost) = each($motor)) {
echo "Kljuc: $kljuc; Vrednost: $vrednost<br />";
}
?>
Rezultat:
Kljuc: 0; Vrednost: suzuki
Kljuc: 1; Vrednost: ktm
Kljuc: 2; Vrednost: honda
Through array you can pass with function count(), which specifies how many elements are in the array.
Example:
<?php
$motorcycle[0] = "suzuki";
$motorcycle[1] = "ktm";
$motorcycle[2] = "honda";
for ($index = 0; $index < count($motorcycle); $index++) {
echo $motorcycle[$index], "<br />";
}
?>
Result:
suzuki
ktm
honda
The function that displays the array element is a function print_r().
Example 2:
<?php
$motorcycle[0] = "suzuki";
$motorcycle[1] = "ktm";
$motorcycle[2] = "honda";
print_r($motorcycle);
?>
Result:
Array ( [0] => suzuki [1] => ktm [2] => honda )
For passing through collections such as arrays you can use foreach statement. This statement has two forms:
foreach (expression_array as $value) statement
This type assigns to the variable $value value of next element from array on each pass through the loop.
foreach (expression_array as $key => $value) statement
This form sets the key of the current element (ie, index) in the variable $key and it works for each pass through the loop.
Example 3: display of all elements of the array using the statement foreach.
<?php
$motorcycle = array("suzuki", "ktm", "honda");
foreach ($motorcycle as $value) {
echo "Value: $value<br />";
}
?>
Result:
Value: suzuki
Value: ktm
Value: honda
Example 4: display of keys and values of an array.
<?php
$motorcycle = array("suzuki", "ktm", "honda");
foreach ($motorcycle as $key => $value) {
echo "Key: $key; Value: $value<br />";
}
?>
Result:
Key: 0; Value: suzuki
Key: 1; Value: ktm
Key: 2; Value: honda
With while loop you can also pass through array, with function each(). This function is used to pass through the collection (ie. arrays). The function each in each pass returns the key of the current element, and then moves on to the next element. If you want to handle multiple values that come from array, you can use the function list(). This function, two returned values assigns to special variables.
Example 5:
<?php
$motorcycle = array("suzuki", "ktm", "honda");
while (list($key, $value) = each($motorcycle)) {
echo "Key: $key; Value: $value<br />";
}
?>
Result:
Key: 0; Value: suzuki
Key: 1; Value: ktm
Key: 2; Value: honda
Primer:
<?php
$motor[0] = "suzuki";
$motor[1] = "ktm";
$motor[2] = "honda";
for ($indeks = 0; $indeks < count($motor); $indeks++) {
echo $motor[$indeks], "<br />";
}
?>
Rezultat:
suzuki
ktm
honda
Funkcija koja prikazuje element niza je funkcija print_r().
Primer 2:
<?php
$motor[0] = "suzuki";
$motor[1] = "ktm";
$motor[2] = "honda";
print_r($motor);
?>
Rezultat:
Array ( [0] => suzuki [1] => ktm [2] => honda )
Za prolaz kroz kolekcije kao sto su nizovi koristite iskaz foreach. Ovaj iskaz ima dva oblika:
foreach (izraz_niza as $vrednost) iskaz
Ovaj oblik dodeljuje promenljivoj $vrednost vrednost sledeceg elementa iz niza pri svakom prolazu kroz petlju.
foreach (izraz_niza as $kljuc => $vrednost) iskaz
Ovaj oblik postavlja kljuc tekuceg elementa (tj. indeks), u promenljivu $kljuc i to radi za svaki prolaz kroz petlju.
Primer 3: prikaz svih elemenata niza pomocu iskaza foreach.
<?php
$motor = array("suzuki", "ktm", "honda");
foreach ($motor as $vrednost) {
echo "Vrednost: $vrednost<br />";
}
?>
Rezultat:
Vrednost: suzuki
Vrednost: ktm
Vrednost: honda
Primer 4: prikaz i kljuceva i vrednosti niza.
<?php
$motor = array("suzuki", "ktm", "honda");
foreach ($motor as $kljuc => $vrednost) {
echo "Kljuc: $kljuc; Vrednost: $vrednost<br />";
}
?>
Rezultat:
Kljuc: 0; Vrednost: suzuki
Kljuc: 1; Vrednost: ktm
Kljuc: 2; Vrednost: honda
Pomocu petlje while takodje mozete da prodjete kroz niz, sa funkcijom each(). Ova funkcija se koristi za prolaz kroz kolekcije (tj. nizove). Funkcija each us vakom prolazu vraca kljuc tekuceg elementa , a onda prelazi na sledeci element. Ako zelite da rukujete visestrukim vrednostima koje se dobijaju iz niza, mozete da koristite funkciju list(). Ova funkcija dve povratne vrednosti dodeljuje posebnim promenljivama.
Primer 5:
<?php
$motor = array("suzuki", "ktm", "honda");
while (list($kljuc, $vrednost) = each($motor)) {
echo "Kljuc: $kljuc; Vrednost: $vrednost<br />";
}
?>
Rezultat:
Kljuc: 0; Vrednost: suzuki
Kljuc: 1; Vrednost: ktm
Kljuc: 2; Vrednost: honda
Through array you can pass with function count(), which specifies how many elements are in the array.
Example:
<?php
$motorcycle[0] = "suzuki";
$motorcycle[1] = "ktm";
$motorcycle[2] = "honda";
for ($index = 0; $index < count($motorcycle); $index++) {
echo $motorcycle[$index], "<br />";
}
?>
Result:
suzuki
ktm
honda
The function that displays the array element is a function print_r().
Example 2:
<?php
$motorcycle[0] = "suzuki";
$motorcycle[1] = "ktm";
$motorcycle[2] = "honda";
print_r($motorcycle);
?>
Result:
Array ( [0] => suzuki [1] => ktm [2] => honda )
For passing through collections such as arrays you can use foreach statement. This statement has two forms:
foreach (expression_array as $value) statement
This type assigns to the variable $value value of next element from array on each pass through the loop.
foreach (expression_array as $key => $value) statement
This form sets the key of the current element (ie, index) in the variable $key and it works for each pass through the loop.
Example 3: display of all elements of the array using the statement foreach.
<?php
$motorcycle = array("suzuki", "ktm", "honda");
foreach ($motorcycle as $value) {
echo "Value: $value<br />";
}
?>
Result:
Value: suzuki
Value: ktm
Value: honda
Example 4: display of keys and values of an array.
<?php
$motorcycle = array("suzuki", "ktm", "honda");
foreach ($motorcycle as $key => $value) {
echo "Key: $key; Value: $value<br />";
}
?>
Result:
Key: 0; Value: suzuki
Key: 1; Value: ktm
Key: 2; Value: honda
With while loop you can also pass through array, with function each(). This function is used to pass through the collection (ie. arrays). The function each in each pass returns the key of the current element, and then moves on to the next element. If you want to handle multiple values that come from array, you can use the function list(). This function, two returned values assigns to special variables.
Example 5:
<?php
$motorcycle = array("suzuki", "ktm", "honda");
while (list($key, $value) = each($motorcycle)) {
echo "Key: $key; Value: $value<br />";
}
?>
Result:
Key: 0; Value: suzuki
Key: 1; Value: ktm
Key: 2; Value: honda
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.