Website Development Prices

Search Blog

Friday, February 19, 2016

Kretanje kroz nizove (Navigating through arrays)

U PHP-u postoji veci broj funkcija koje se koristi za kretanje kroz nizove. Ovo kretanje se vrsi pomocu pokazivaca niza, koji sadrzi tekucu lokaciju u nizu. 

Primer:

<?php

$motori[0] = "suzuki";
$motori[1] = "ducati";
$motori[2] = "ktm";

print_r($motori);

/* ako zelite da odredite tekuci element niza 
mozete koristiti funkciju current(). */
echo "Trenutni: ", current($motori), "<br />";

/* ako zelite da prebacite pokazivac na 
sledeci element, koristite funkciju next() */
echo "Sledeci: ", next($motori), "<br />";

/* ako zelite da prebacite pokazivac na 
prethodni element, koristite funkciju prev(). */
echo "Prethodni: ", prev($motori), "<br />";

/* ako zelite da prebacite pokazivac na 
poslednji element, koristite funkciju end(). */
echo "Poslednji: ", end($motori), "<br />";

/* ako zelite da se vratite na pocetak niza, 
koristite funkciju reset(), ili ... */
echo "Na pocetak: ", reset($motori), "<br />";

/* ... ili, ako zelite da se vratite na pocetak niza, 
koristite funkciju reset(). */
echo "Resetovanje niza. <br />";

/* ako zelite da prikazete novi tekuci element, 
koji je sada prvi element niza, 
koristite funkciju current(). */
echo "Trenutni: ", current($motori), "<br />";

?> 

Rezultat:

Array ( [0] => suzuki [1] => ducati [2] => ktm ) Trenutni: suzuki
Sledeci: ducati
Prethodni: suzuki
Poslednji: ktm
Na pocetak: suzuki
Resetovanje niza.
Trenutni: suzuki


In PHP there are a number of functions that are used to navigate through arrays. This navigating is done using the array pointer, which contains the current location in the array.

Example:

<?php

$motorcycles[0] = "suzuki";
$motorcycles[1] = "ducati";
$motorcycles[2] = "ktm";

print_r($motorcycles);

/* if you want to specify the current element of an array
you can use function current(). */
echo "Current: ", current($motorcycles), "<br />";

/* if you want to move a pointer to
the next element, you can use function next() */
echo "Next: ", next($motorcycles), "<br />";

/* if you want to move a pointer to
the previous element, you can use function prev(). */
echo "Previous: ", prev($motorcycles), "<br />";

/* if you want to move a pointer to
the end of element, you can use function end(). */
echo "End: ", end($motorcycles), "<br />";

/* if you want to return to the beginning, 
of array, you can use function reset(), or ... */
echo "On the beginning: ", reset($motorcycles), "<br />";

/* ... or, if you want to return to the beginning, 
of array, you can use function reset() */
echo "Resetting the array. <br />";

/* if you want to display new current element, 
which is now the first element of array, 
you can use function current(). */
echo "Current: ", current($motorcycles), "<br />";

?>

Result:

Array ( [0] => suzuki [1] => ducati [2] => ktm ) Current: suzuki
Next: ducati
Previous: suzuki
End: ktm
On the beginning: suzuki
Resetting the array. 
Current: suzuki 

No comments:

Post a Comment

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