Website Development Prices

Search Blog

Tuesday, February 9, 2016

Izmena nizova (Modification of arrays)

Ako ste vec napravili niz, postoji mogucnost promene vrednosti u nizu. Jedan od nacina je da elementu niza pristupite pomocu indeksa. 

Primer: ako zelite da vrednost za $motor[3] promenite na "ducati".

<?php

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

$motor[3] = "ducati";

?>

Rezultat:

ducati

Ako posle toga zelite da dodate novi element "kawasaki" i da on treba da ide na kraj niza. Ovo mozete da uradite pomocu precice za dodavanje novog elementa, a to je iskaz $motor[].

Primer 2:

<?php

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

$motor[2] = "ducati";

$motor[] = "kawasaki";


?>

Da prikazete sadrzaj treba da prodjete kroz niz.

Primer 3:

<?php

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

$motor[2] = "ducati";

$motor[] = "kawasaki";

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



?>

Rezultat:

suzuki
ktm
ducati
kawasaki


Ako zelite mozete i da ceo niz kopirate u drugi niz.

Primer 4:

<?php

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

$italijaProizvodi = $motor;

echo $italijaProizvodi[2];


?>

Rezultat:

ducati

If you have already made array, there is a possibility to change the value in the array. One of the ways is that, to access the element of the array you use the index.

For example, if you want a value for $motorcycle[3] to change to "ducati".

<?php

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

$motorcycle[3] = "ducati";

?>

Result:

ducati

If after that you want to add a new element "kawasaki" and that he needs to go to the end of the array. This you can do with shortcut for adding a new element, and that is the statement $motorcycle[].

Example 2:

<?php

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

$motorcycle[2] = "ducati";

$motorcycle[] = "kawasaki";

?>

To display the content you should go through array.

Example 3:

<?php

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

$motorcycle[2] = "ducati";

$motorcycle[] = "kawasaki";

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

}

?>

Result:

suzuki
ktm
ducati
kawasaki

If you want you can copy whole array in the 2nd array.

Example 4:

<?php

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

$italyProduce = $motorcycle;

echo $italyProduce[2];


?>

Result:

ducati

No comments:

Post a Comment

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