Website Development Prices

Search Blog

Wednesday, February 24, 2016

Poredjenje nizova (Comparing arrays)

PHP podrzava poredjenje nizova i odredjivanje istih elemenata ili razlicitih. 

Pimer: ako imate sledeca dva niza i ako je u njima drugi element isti.

<?php

$japan = array("honda", "suzuki", "kawasaki");
$kina = array("shineray", "suzuki", "thumpstar");



?>

Pomocu funkcije array_diff() mozete napraviti novi niz. U ovom primeru dacemo ime $razlicito i u njemu ce se naci elementi koji su u ova dva niza razliciti.

Nastavak primera: 


<?php

$japan = array("honda", "suzuki", "kawasaki");
$kina = array("shineray", "suzuki", "thumpstar");

$razlicito = array_diff($japan, $kina);

foreach ($razlicito as $kljuc => $vrednost) {
echo "Kljuc: $kljuc; Vrednost: $vrednost<br />";
}


?>

Rezultat:

Kljuc: 0; Vrednost: honda

Kljuc: 2; Vrednost: kawasaki

Ako radite sa dva niza koja koriste tekstualne indekse i ako zelite da vidite koji elementi imaju razlicite kljuceve ili vrednosti.

Primer 2:

<?php

$japan = array("motor1" => "honda", "motor2" => "suzuki",
"motor3" => "kawasaki");

$kina = array("motor1" => "shineray", "motor_dva" => "suzuki",
"motor3" => "thumpstar");

$rezultat = array_diff($japan, $kina);

print_r($rezultat);

?>

Rezultat:

Array ( [motor1] => honda [motor3] => kawasaki )

Pomocu funkcije array_diff_assoc() mozete odrediti elemente sa razlicitim kljucem ili vrednostima. Nizovi sa tekstualnim indeksima se nazivaju asocijativni nizovi.

Primer 3:

<?php

$japan = array("motor1" => "honda", "motor2" => "suzuki",
"motor3" => "kawasaki");

$kina = array("motor1" => "shineray", "motor_dva" => "suzuki",
"motor3" => "thumpstar");

$razlicito = array_diff_assoc($japan, $kina);

foreach ($razlicito as $kljuc => $vrednost) {
echo "Kljuc: $kljuc; Vrednost: $vrednost<br />";
}

?>

Rezultat:

Kljuc: motor1; Vrednost: honda
Kljuc: motor2; Vrednost: suzuki
Kljuc: motor3; Vrednost: kawasaki


Ako zelite da pronadjete sve elemente niza koji su zajednicki, koristite funkciju array_intersect().

Primer 4: pronalazimo elemente koji su zajednicki u dva niza.

<?php

$japan = array("honda", "suzuki", "kawasaki");
$kina = array("shineray", "suzuki", "thumpstar");

$zajednicko = array_intersect($japan, $kina);

foreach ($zajednicko as $kljuc => $vrednost) {
echo "Kljuc: $kljuc; Vrednost: $vrednost<br />";

}

?>

Rezultat:

Kljuc: 1; Vrednost: suzuki

Mozete isto uraditi za nizove koji koriste tekstualne indekse, pomocu funkcije array_intersect_assoc().

Primer 5:

<?php

$japan = array("motor1" => "honda", "motor2" => "suzuki",
"motor3" => "kawasaki");

$kina = array("motor1" => "shineray", "motor2" => "suzuki",
"motor3" => "thumpstar");

$zajednicko = array_intersect($japan, $kina);

foreach ($zajednicko as $kljuc => $vrednost) {
echo "Kljuc: $kljuc; Vrednost: $vrednost<br />";

}

?>

Rezultat:

Kljuc: motor2; Vrednost: suzuki

PHP supports comparison of arrays and determination of the same elements or different.

Example: if you have the next two arrays and if the second element in them is the same.

<?php

$japan = array("honda", "suzuki", "kawasaki");

$china = array("shineray", "suzuki", "thumpstar");



?>

With the help of function array_diff() you can make a new array. In this example, we'll give it name $difference and in it will be the elements that are in the two arrays different.

Continued example:

<?php

$japan = array("honda", "suzuki", "kawasaki");
$china = array("shineray", "suzuki", "thumpstar");

$difference = array_diff($japan, $china);

foreach ($difference as $key => $value) {
echo "Key: $key; Value: $value<br />";

}

?>

Result:

Key: 0; Value: honda
Key: 2; Value: kawasaki


If you work with two arrays that use text indexes and if you want to see which elements have different keys or values.

Example 2:

<?php

$japan = array("motorcycle1" => "honda", "motorcycle2" => "suzuki",
"motorcycle3" => "kawasaki");

$china = array("motorcycle1" => "shineray", "motorcycle_two" => "suzuki",
"motorcycle3" => "thumpstar");

$result = array_diff($japan, $china);

print_r($result);

?>

Result:

Array ( [motorcycle1] => honda [motorcycle3] => kawasaki )

With help of function array_diff_assoc() you can determine the elements with different keys or values. Arrays with text indexes are called associative arrays.

Example 3:

<?php

$japan = array("motorcycle1" => "honda", "motorcycle2" => "suzuki",
"motorcycle3" => "kawasaki");

$china = array("motorcycle1" => "shineray", "motorcycle_two" => "suzuki",
"motorcycle3" => "thumpstar");

$difference = array_diff_assoc($japan, $china);

foreach ($difference as $key => $value) {
echo "Key: $key; Value: $value<br />";

}

?>

Result:

Key: motorcycle1; Value: honda
Key: motorcycle2; Value: suzuki
Key: motorcycle3; Value: kawasaki


If you want to find all the elements of the array that are common, use function array_intersect().

Example 4: find elements that are common in the two arrays.

<?php

$japan = array("honda", "suzuki", "kawasaki");
$china = array("shineray", "suzuki", "thumpstar");

$common = array_intersect($japan, $china);

foreach ($common as $key => $value) {
echo "Key: $key; Value: $value<br />";

}

?>

Result:

Key: 1; Value: suzuki

You can do the same for arrays that use text indexes, with the function array_intersect_assoc().

Example 5:

<?php

$japan = array("motorcycle1" => "honda", "motorcycle2" => "suzuki",
"motorcycle3" => "kawasaki");

$china = array("motorcycle1" => "shineray", "motorcycle2" => "suzuki",
"motorcycle3" => "thumpstar");

$common = array_intersect($japan, $china);

foreach ($common as $key => $value) {
echo "Key: $key; Value: $value<br />";

}

?>

Result:

Key: motorcycle2; Value: suzuki

No comments:

Post a Comment

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