Website Development Prices

Search Blog

Friday, March 4, 2016

Kreiranje visedimenzionalnih nizova (Creating multidimensional arrays)

Do sada smo radili sa jednodimenzionalnim nizovima. Moguce je napraviti i nizove sa vise skupova kljuceva. 

Primer: mozete da skupove cena motora za razlicite motore sacuvate na sledeci nacin.

<?php

$cene_motora["suzuki"] = 2500; 

$cene_motora["lifan"] = 3700;

?>

Ako imate jos jednu cenu motora, mozete da dodate drugi indeks koji oznacava broj motora.

Nastavak primera:

<?php

$cene_motora["suzuki"] [1] = 2500;
$cene_motora["suzuki"] [2] = 1900;
$cene_motora["honda"] [1] = 3700;
$cene_motora["honda"] [2] = 2550;


print_r($cene_motora)

?>


Sacuvajte ovaj fajl kao visedimenzionalni-nizovi.php.

Rezultat:

Array ( [suzuki] => Array ( [1] => 2500 [2] => 1900 ) [honda] => Array ( [1] => 3700 [2] => 2550 ) )

Objasnjenje: sada se u promenljivoj $cene_motora["suzuki"] [1] nalazi cena motora suzuki iz jedne radnje, u promenljivoj $cene_motora["suzuki"] [2] je cena drugog motora suzuki, u drugoj radnji. 

Pojedinacnim elementima niza mozete pristupiti pomocu oba indeksa.

Primer 2:

>?php

$cene_motora["suzuki"] [1] = 2500;
$cene_motora["suzuki"] [2] = 1900;
$cene_motora["honda"] [1] = 3700;
$cene_motora["honda"] [2] = 2550;

echo "Cena motora suzuki u prvoj radnji je ", 

$cene_motora["suzuki"] [1], "<br />";

?>

Rezultat:

Cena motora suzuki u prvoj radnji je 2500

Ako zelite da ubacite stavku niza sa dvostrukim navodnicima, stavite je u viticaste zagrade {}, a za tekstualne kjuceve koristite jednostruke navodnike da izbegnete konflikt sa dvostrukim navodnicima.

Primer 3:

<?php

$cene_motora["suzuki"] [1] = 2500;
$cene_motora["suzuki"] [2] = 1900;
$cene_motora["honda"] [1] = 3700;
$cene_motora["honda"] [2] = 2550;

echo "Cena motora suzuki u prvoj radnji je 

{$cene_motora['suzuki'] [1]}<br />";

?>

Rezultat:

Cena motora suzuki u prvoj radnji je 2500

Za kreiranje visedimenzionalnih nizova mozete koristiti sledecu sintaksu.

Napomena: na ovaj nacin vrednost indeksa pocinje od nula.

Primer 4:

<?php

$cene_motora["suzuki"] [] = 2500;
$cene_motora["suzuki"] [] = 1900;
$cene_motora["honda"] [] = 3700;
$cene_motora["honda"] [] = 2550;


print_r($cene_motora);

?>

Rezultat:

Array ( [suzuki] => Array ( [0] => 2500 [1] => 1900 ) [honda] => Array ( [0] => 3700 [1] => 2550 ) )

U PHP-u visedimenzionalne nizove mozete posmatrati kao nizove nizova.

Primer 5: dvodimenzionalni niz se moze smatrati jednodimenzionalnim, kod kojeg je svaki element drugi jednodimenzionalni niz.

<?php

$cene_motora = array("suzuki" => array(2500, 1900), 
"honda" => array(3700, 2550));

print_r($cene_motora);

?>

Rezultat:

Array ( [suzuki] => Array ( [0] => 2500 [1] => 1900 ) [honda] => Array ( [0] => 3700 [1] => 2550 ) )

Ako zelite da niz pocne sa indeksom 1 a ne sa 0, mozete uraditi sledece:

Primer 6:

<?php

$cene_motora = array("suzuki" => array(1 => 2500, 2 => 1900), 
"honda" => array(1 => 3700, 2 => 2550));

print_r($cene_motora);

?>

Rezultat:

Array ( [suzuki] => Array ( [1] => 2500 [2] => 1900 ) [honda] => Array ( [1] => 3700 [2] => 2550 ) )

So far we have worked with one-dimensional arrays. It is possible to make arrays with more sets of keys.

Example: you can sets of the prices of motorcycles for different motorcycles, save in the following way.

<?php

$motorcycle_prices["suzuki"] = 2500; 

$motorcycle_prices["honda"] = 3700;

?>

If you have another price of motorcycles, you can add another index that indicates the number of motorcycle.

Example continued:

<?php

$motorcycle_prices["suzuki"] [1] = 2500;
$motorcycle_prices["suzuki"] [2] = 1900;
$motorcycle_prices["honda"] [1] = 3700;
$motorcycle_prices["honda"] [2] = 2550;


print_r($motorcycle_prices)

?>


Save this file as a multidimensional-arrays.php.

Result:

Array ( [suzuki] => Array ( [1] => 2500 [2] => 1900 ) [honda] => Array ( [1] => 3700 [2] => 2550 ) )

Explanation: now in the variable $motorcycle_prices["suzuki"] [1] is motorcycle price of suzuki from one store, in the variable $motorcycle_prices["suzuki"] [2]  is motorcycle price of suzuki from second store

Individual elements of an array can be accessed using both indexes.

Example 2:

<?php

$motorcycle_prices["suzuki"] [1] = 2500;
$motorcycle_prices["suzuki"] [2] = 1900;
$motorcycle_prices["honda"] [1] = 3700;
$motorcycle_prices["honda"] [2] = 2550;

echo "Price of motorcycle suzuki in first store is ", 

$motorcycle_prices["suzuki"] [1], "<br />";

?>

Result:

Price of motorcycle suzuki in first store is 2500

If you want to insert the item of array with double quotes, place it in curly braces {}, and for text keys, use single quotes to avoid conflict with the double quotes.

Example 3:

<?php

$motorcycle_prices["suzuki"] [1] = 2500;
$motorcycle_prices["suzuki"] [2] = 1900;
$motorcycle_prices["honda"] [1] = 3700;
$motorcycle_prices["honda"] [2] = 2550;

echo "Price of motorcycle suzuki in first store is 

{$motorcycle_prices['suzuki'] [1]}<br />";

?>

Result:

Price of motorcycle suzuki in first store is 2500

To create a multi-dimensional arrays, you can use the following syntax.

Note: in this way the value of the index starts from zero.

Example 4:

<?php

$motorcycle_prices["suzuki"] [] = 2500;
$motorcycle_prices["suzuki"] [] = 1900;
$motorcycle_prices["honda"] [] = 3700;
$motorcycle_prices["honda"] [] = 2550;


print_r($motorcycle_prices);

?>

Result:

Array ( [suzuki] => Array ( [0] => 2500 [1] => 1900 ) [honda] => Array ( [0] => 3700 [1] => 2550 ) )

In PHP multidimensional arrays you can view as a arrays of arrays.

Example 5: two-dimensional array can be considered as one-dimensional, in which each element is the second one-dimensional array.

<?php

$motorcycle_prices = array("suzuki" => array(2500, 1900), 
"honda" => array(3700, 2550));

print_r($motorcycle_prices);

?>

Result:

Array ( [suzuki] => Array ( [0] => 2500 [1] => 1900 ) [honda] => Array ( [0] => 3700 [1] => 2550 ) )

If you want to start array with an index of 1 and not 0, you can do the following:

Example 6:

<?php

$motorcycle_prices = array("suzuki" => array(1 => 2500, 2 => 1900), 
"honda" => array(1 => 3700, 2 => 2550));

print_r($motorcycle_prices);

?>

Result:

Array ( [suzuki] => Array ( [1] => 2500 [2] => 1900 ) [honda] => Array ( [1] => 3700 [2] => 2550 ) )

No comments:

Post a Comment

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