Website Development Prices

Search Blog

Sunday, February 21, 2016

Vadjenje promenljivih iz niza (Extracting variables from an array)

Funkcija extract je korisna za kopiranje elemenata nizova u promenljive ako niz radi sa indeksima u obliku stringa. 

Primer: kada za niz pozovemo funkciju extract, kreiraju se promenljive koje odgovaraju indeksima $japan, $italija, $austrija.

<?php

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

extract($motor);
.
.
.

?>

Primer 2:

<?php

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

extract($motor);

echo "\$japan = $japan<br />";
echo "\$italija = $italija<br />";
echo "\$austrija = $austrija<br />";

?>

Rezultat:

$japan = suzuki
$italija = ducati
$austrija = ktm


Ako zelite da izvadite podatke iz niza mozete da koristite funkciju list(). Rezultat se moze smestati u razlicite promenljive.

Primer 3:

<?php

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

list($prvi, $drugi) = $motori;

echo $prvi, "<br />";
echo $drugi;

?>

Rezultat:

suzuki
ducati


Ako zelite da uradite suprotno i da vrednost iz promenljivih smestite u niz, koristite funkciju compact(). Ovoj funkciji prosledjujete nizove promenljivih, sa $, a funkcija pronalazi promenljive i smesta ih u niz.

Primer 4:

<?php

$ime = "Miran";
$prezime = "Stanovnik";
$zanimanje = "motociklista";

$punoime = array("ime", "prezime");
$rezultat = compact($punoime, "zanimanje");


print_r($rezultat);

?>

Rezultat:

Array ( [ime] => Miran [prezime] => Stanovnik [zanimanje] => motociklista )

The function extract is useful for copying elements of arrays in a variables if array works with indexes in the form of string.

Example, when you for an array call function extract, it creates the variables that correspond to the indexes $japan, $italy, $austria.

<?php

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

extract($motorcycle);

.
.

.

?>

Example 2:

<?php

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

extract($motorcycle);

echo "\$japan = $japan<br />";
echo "\$italy = $italy<br />";
echo "\$austria = $austria<br />";

?>

Result:

$japan = suzuki
$italy = ducati
$austria = ktm

If you want to extract data from array you can use function list(). The result can be stored in a different variables.

Example 3:

<?php

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

list($first, $second) = $motorcycles;

echo $first, "<br />";
echo $second;

?>

Result:

suzuki
ducati

If you want to do the opposite and that the value from variables store in array, use function compact(). To this function you forward arrays from variables, with $, and the function finds variables and places them in array.

Example 4:

<?php

$name = "Miran";
$lastname = "Stanovnik";
$proffesion = "motorcyclist";

$fullname = array("name", "lastname");
$result = compact($fullname, "proffesion");


print_r($result);

?>

Result:

Array ( [name] => Miran [lastname] => Stanovnik [proffesion] => motorcyclist )

No comments:

Post a Comment

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