Website Development Prices

Search Blog

Sunday, February 14, 2016

Brisanje elemenata iz niza (Removing elements from array)

U sledecem primeru, element se ne brise vec se u njemu skladisti prazan string.

Primer:

<?php

$motor[0] = "suzuki";
$motor[1] = "ktm";
$motor[2] = "honda"; 

$motor[1] = "";

for ($indeks = 0; $indeks < count($motor); $indeks++) {
echo $motor[$indeks], "<br />";
}


?>

Sacuvajte ovaj fajl kao brisanje-elemenata-iz-niza.php.

Rezultat:

suzuki

honda


Ako zelite da iz niz obrisete niz, koristite funkciju unset().

unset($vrednost[1]);

Primer 2:

<?php

$motor[0] = "suzuki";
$motor[1] = "ktm";
$motor[2] = "honda"; 

unset($motor[1]);

for ($indeks = 0; $indeks < count($motor); $indeks++) {
echo $motor[$indeks], "<br />";
}


?>

Objasnjenje: ako sada pokusate da prikazete element koji je izbacen dobicete upozorenje.

Rezultat:

suzuki

Notice: Undefined offset: 1 in C:\xampp2\htdocs\PHPtuts\brisanje-elemenata-iz-niza.php on line 34


In the following example, the element is not deleted but empty string is stored in it..

Example:

<?php

$motorcycle[0] = "suzuki";
$motorcycle[1] = "ktm";
$motorcycle[2] = "honda"; 

$motorcycle[1] = "";

for ($index = 0; $index< count($motorcycle); $index++) {
echo $motorcycle[$index], "<br />";

}

?>

Save this file as removing-elements-from-array.php.

Result:

suzuki

honda

If you want to delete array from array, use the function unset().

unset ($value [1]);

Example 2:

<?php

$motorcycle[0] = "suzuki";
$motorcycle[1] = "ktm";
$motorcycle[2] = "honda"; 

unset($motorcycle[1]);

for ($index = 0; $index < count($motorcycle); $index++) {
echo $motorcycle[$index], "<br />";
}


?>

Explanation: if you now try to display an element that was removed you'll get a warning.

Result:

suzuki

Notice: Undefined offset: 1 in C:\xampp2\htdocs\PHPtuts\removing-elements-from-array.php on line 34

No comments:

Post a Comment

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