Website Development Prices

Search Blog

Monday, February 22, 2016

Spajanje i deoba nizova (Merging and slicing arrays)

Nizove mozete spajati i deliti. 

Primer: ako imate niz sa tri stavke od kojih svaka predstavlja neki motor, a zelite da dobijete podniz koji sadrzi samo poslednje dve stavke, mozete koristiti funkciju array_slice(). Ovoj funkciji prosledjujete niz ciji deo zelite da dobijete. Ova funkcija prima argument offset, koji pokazuje odakle treba poceti, i argument length, koji ukazuje na duzinu niza koji zelite da napravite.

<?php

$motor["japan"] = "suzuki";
$motor["italija"] = "ducati";
$motor["austrija"] = "ktm";

$podniz = array_slice($motor, 1, 2);

foreach ($podniz as $vrednost) {
echo "Motor: $vrednost<br />";

}

?>

Rezultat:

Motor: ducati
Motor: ktm


Ako je vrednost argumenta offset negativna, pocetak se broji od kraja niza. Ako je length negativna vrednost, elementi se uzimaju od kraja niza. 

Ako ne zadate duzinu podniza, dobicete sve elemente do kraja (ili do pocetka, ako idete u suprotnom smeru).

Primer 2: ako zelite da spojite dva ili vise nizova, koristite funkciju array_merge().

<?php

$motori = array("suzuki", "ducati", "ktm");
$automobili = array("jugo", "tojota", "pezo");

$proizvodnja = array_merge($motori, $automobili);

foreach ($proizvodnja as $vrednost) {
echo "Proizvodnja prevoznog sredstva: $vrednost<br />";

}

?>

Rezultat:

Proizvodnja prevoznog sredstva: suzuki
Proizvodnja prevoznog sredstva: ducati
Proizvodnja prevoznog sredstva: ktm
Proizvodnja prevoznog sredstva: jugo
Proizvodnja prevoznog sredstva: tojota
Proizvodnja prevoznog sredstva: pezo


You can merge and slice arrays.

Example, if you have an array with three items, each of which represents one motorcycle, and you wish to get a subarray containing only last two items, you can use function array_slice(). To this function you forward array whose part you want to get. This function accepts an argument offset, which shows where to start, and argument length, which indicates the length of array you want to make.

<?php

$motorcycle["japan"] = "suzuki";
$motorcycle["italy"] = "ducati";
$motorcycle["austria"] = "ktm";

$subarray = array_slice($motorcycle, 1, 2);

foreach ($subarray as $value) {
echo "Motorcycle: $value<br />";

}

?>

Result:

Motorcycle: ducati
Motorcycle: ktm


If the value of the argument offset is negative, start is counted from the end of the array. If the length has negative value, the elements are taken from the end of the array.

If you do not specify the length of the subarray, you will get all the elements to the end (or to the beginning, if you're going in the opposite direction).

Example 2: if you want to merge two or more arrays, use function array_merge().

<?php

$motorcycles = array("suzuki", "ducati", "ktm");
$cars = array("yugo", "toyota", "peugeot");

$production = array_merge($motorcycles, $cars);

foreach ($production as $value) {
echo "Production of transportation: $value<br />";

}

?>

Result:

Production of transportation: suzuki
Production of transportation: ducati
Production of transportation: ktm
Production of transportation: yugo
Production of transportation: toyota
Production of transportation: peugeot


No comments:

Post a Comment

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