Website Development Prices

Search Blog

Thursday, February 4, 2016

Pravljenje nizova (Creating arrays)

Nizovi su kolekcije vrednosti koje se cuvaju pod jednim imenom. Njih cete koristiti kada budete radili sa skupovima podataka. Svakom elementu niza moze se pristupiti pomocu indeksa.

Nizove mozete praviti dodelom podataka. Takodje, mozete im davati ista imena kao i standardnim promenljivama. Nazivi nizova pocinju oznakom dolar $. PHP zna da je rec o nizovima ako iza imena stavite uglaste zagrade [].

Primer:

<?php

$motor[1] = "suzuki";
echo $motor[1];


?>

Rezultat:

suzuki

Objasnjenje:  prvom linijom koda, pravimo niz sa nazivom $motor i element 1 se podesavana na "suzuki". Druga linija koda, tom elementu - [1], mozete da pristupite tako sto cete da ukljucite indeks (numericki indeks). Ovaj iskaz prikazuje tekst "suzuki". 

Ako zelite da dodate nove vrednosti, to mozete postici dodavanjem novih indeksa.

Primer 2:

<?php

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

echo $motor[1], "<br />";
echo $motor[2], "<br />";
echo $motor[3], "<br />";

?>

Rezultat:

suzuki
ktm
honda


Objasnjenje: u ovom primeru smo stringove ubacili pomocu numerickog indeksa.

Mozete koristiti i indekse u vidu stringova.

Primer 3:

<?php

$inventar_dukati_motora["Diavel"] = 154;
$inventar_dukati_motora["Hypermotard"] = 345;
$inventar_dukati_motora["Monster"] = 147;

echo $inventar_dukati_motora["Diavel"], "<br />";
echo $inventar_dukati_motora["Hypermotard"], "<br />";

echo $inventar_dukati_motora["Monster"], "<br />";

?>

Rezultat:

154
345
147


Mozete koristiti i precicu za pravljenje nizova, tak sto cete posle imena niza dodati prazne uglaste zagrade [].

Primer 4:

<?php

$motor[] = "suzuki";
$motor[] = "ktm";
$motor[] = "honda";

echo $motor[0], "<br />";
echo $motor[1], "<br />";

echo $motor[2], "<br />";

?>

Rezultat:

suzuki
ktm
honda


Objasnjenje: brojanje elemenata u nizu u PHP-u pocinje od 0 (nule).

Ako zelite da pomocu petlje prodjete kroz sve elemente niza, treba da pocnete sa 0 (nula). 


Primer 5:

<?php

$motori = array("ducati", "yamaha", "kawasaki");

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



?>

Rezultat:

ducati
yamaha
kawasaki


Objasnjenje: ovde indeksiranje pocinje od 0 (nule).

Ako zelite da indeks pocne od 1, koristite operator =>.

Primer 6:

<?php

$motori = array(1 => "suzuki", "ktm", "honda");

echo $motori[1], "<br />";
echo $motori[2], "<br />";

echo $motori[3], "<br />";

?>

Rezultat:

suzuki
ktm
honda


Ovaj niz mozete da napravite i pomocu tekstualnih indeksa.

Primer 7:

<?php

$inventar_dukati_motora = array(
"Diavel" => 257, "Hypermotard" => 541, "Monster" => 365);

echo $inventar_dukati_motora["Diavel"], "<br />";
echo $inventar_dukati_motora["Hypermotard"], "<br />";

echo $inventar_dukati_motora["Monster"], "<br />";

?>

Rezultat:

257
541
365


Objasnjenje: operator => omogucava da zadate parove kljuc/vrednost. Npr. "Diavel" je kljuc za prvi element, a 257 je njegova vrednost.

Ako zelite da odredite duzinu niza koristite funkciju count(), koja se koristi za vracanje duzine (broj elemenata) od niza. 

Primer 8:

<?php

$motori = array("ducati", "yamaha", "kawasaki", "bmw");

echo count($motori);

?>

Rezultat:

4

Arrays are collections of values ​​that are stored under one name. You will use them when you work with data sets. Each element of the array can be accessed using the index.

You can create arrays by adding data. Also, you can give them the same names as the standard variables. The names of strings begin with dollar sign $. PHP knows that this is an array if after the name you put square brackets [].

Example:

<?php

$motorcycle[1] = "suzuki";

echo $motorcycle[1];

?>

Result:

suzuki

Explanation: with the first line of code, we create an array with name $motorcycles and element 1 is adjusted to the "suzuki". The second line of code, this element - [1], you can access by adding index (numerical index). This statement displays the text "suzuki".

If you want to add a new values, this can be achieved by adding a new indexes.

Example 2:

<?php

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

echo $motorcycle[1], "<br />";
echo $motorcycle[2], "<br />";

echo $motorcycle[3], "<br />";


?>

Result:

suzuki
ktm
honda


Explanation: in this case we inserted strings with the help of numeric index.

You can use the indexes as a strings.

Example 3:

<?php

$inventory_of_ducati_bikes["Diavel"] = 154;
$inventory_of_ducati_bikes["Hypermotard"] = 345;
$inventory_of_ducati_bikes["Monster"] = 147;

echo $inventory_of_ducati_bikes["Diavel"], "<br />";
echo $inventory_of_ducati_bikes["Hypermotard"], "<br />";

echo $inventory_of_ducati_bikes["Monster"], "<br />";


?>

Result:

154
345
147

You can use a shortcut to create arrays, so after the name of array you will add empty square brackets [].

Example 4:

<?php

$motorcycle[] = "suzuki";
$motorcycle[] = "ktm";
$motorcycle[] = "honda";

echo $motorcycle[0], "<br />";
echo $motorcycle[1], "<br />";

echo $motorcycle[2], "<br />";


?>

Result:

suzuki
ktm
honda

Explanation: counting the elements in array in PHP starts from 0 (zero).

If you want with the help of the loop to go through all the elements of the array, you should start with 0 (zero).


Example 5:

<?php

$motorcycles = array("ducati", "yamaha", "kawasaki");

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





?>

Result:

ducati
yamaha
kawasaki

Explanation: here indexing starts from 0 (zero).

If you want index to start from 1, you can use operator =>.

Example 6:

<?php

$motorcycles = array(1 => "suzuki", "ktm", "honda");

echo $motorcycles[1], "<br />";
echo $motorcycles[2], "<br />";

echo $motorcycles[3], "<br />";


?>

Result:

suzuki
ktm
honda

This array you can create with the help of string index.

Example 7:

<?php

$inventory_of_ducati_bikes = array(
"Diavel" => 257, "Hypermotard" => 541, "Monster" => 365);

echo $inventory_of_ducati_bikes["Diavel"], "<br />";
echo $inventory_of_ducati_bikes["Hypermotard"], "<br />";

echo $inventory_of_ducati_bikes["Monster"], "<br />";


?>

Result:

257
541
365

Explanation: operator => allows you to specify key/value pairs. Eg. "Diavel" is the key to the first element, and 257 is its value.

If you want to specify the length of the array, use the count() function, which is used for the return of the length (number of elements) of the array.

Example 8:

<?php

$motorcycles = array("ducati", "yamaha", "kawasaki", "bmw");

echo count($motorcycles);


?>

Result:


4

No comments:

Post a Comment

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