Website Development Prices

Search Blog

Friday, March 11, 2016

Upotreba operatora za rad sa nizovima (Using the operators to work with arrays)

Operatori za rad sa nizovima:



Primer: proveravamo da li dva niza imaju iste elemente.

<?php

$japan["suzuki"] = 15;  
$japan["honda"] = 19; 
$kina["shineray"] = 11; 
$kina["thumpstar"] = 17; 

echo "\$japan: ";
print_r($japan);
echo "<br />";
echo "\$kina: ";
print_r($kina);
echo "<br />";

$motori = $japan + $kina;
echo "\$motori: ";
print_r($motori);
echo "<br />";

if ($japan == $kina) {
echo "\$japan ima iste elemente kao \$kina<br />";
} else {
echo "\$japan nema iste elemente kao \$kina<br />";
}

?>


Rezultat: nemaju iste elemente.

$japan: Array ( [suzuki] => 15 [honda] => 19 )
$kina: Array ( [shineray] => 11 [thumpstar] => 17 )
$motori: Array ( [suzuki] => 15 [honda] => 19 [shineray] => 11 [thumpstar] => 17 )
$japan nema iste elemente kao $kina


Objasnjenje: operator + spaja nizove, a operator == poredi nizove.

Operators to work with arrays:



Example: checking whether two arrays have the same elements.

<?php

$japan["suzuki"] = 15;  
$japan["honda"] = 19; 
$china["shineray"] = 11; 
$china["thumpstar"] = 17; 

echo "\$japan: ";
print_r($japan);
echo "<br />";
echo "\$china: ";
print_r($china);
echo "<br />";

$motorcycles = $japan + $china;
echo "\$motorcycles: ";
print_r($motorcycles);
echo "<br />";

if ($japan == $china) {
echo "\$japan has the same elements as \$china<br />";
} else {
echo "\$japan does not have the same elements as \$china<br />";

}

?>

Result: they don't have the same elements.

$japan: Array ( [suzuki] => 15 [honda] => 19 )
$china: Array ( [shineray] => 11 [thumpstar] => 17 )
$motorcycles: Array ( [suzuki] => 15 [honda] => 19 [shineray] => 11 [thumpstar] => 17 )
$japan does not have the same elements as $china



Explanation: operator + combines arrays and the operator == compares arrays.

No comments:

Post a Comment

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