Ako zelite da obrisete duple elemente koristite funkciju array_unique().
Primer:
<?php
$cene_motora = array(3500, 3700, 2300, 3500, 3500);
print_r($cene_motora);
$cene = array_unique($cene_motora);
print_r($cene_motora);
?>
Rezultat:
Array ( [0] => 3500 [1] => 3700 [2] => 2300 [3] => 3500 [4] => 3500 ) Array ( [0] => 3500 [1] => 3700 [2] => 2300 )
Ako zelite da saberete sve vrednosti koje se nalaze u nizu, koristite funkciju array_sum().
Primer 2:
<?php
$cene_motora = array(3500, 3700, 2300, 3500, 3500);
echo "Prosecna cena = ", array_sum($cene_motora) / count($cene_motora);
?>
Rezultat:
Prosecna cena = 3300
Objasnjenje: pronasli smo srednju cenu motora. Cene motora se nalaze u nizu $cene_motora.
Funkcija array_flip() menja mesta kljucevima i vrednostima.
Primer 3:
<?php
$japan = array("motor1" => "honda", "motor2" => "suzuki",
"motor3" => "kawasaki");
foreach ($japan as $kljuc => $vrednost) {
echo "Kljuc: $kljuc; Vrednost: $vrednost<br />";
}
echo "<br />";
$japan = array_flip($japan);
foreach ($japan as $kljuc => $vrednost) {
echo "Kljuc: $kljuc; Vrednost: $vrednost<br />";
}
?>
Rezultat:
Kljuc: motor1; Vrednost: honda
Kljuc: motor2; Vrednost: suzuki
Kljuc: motor3; Vrednost: kawasaki
Kljuc: honda; Vrednost: motor1
Kljuc: suzuki; Vrednost: motor2
Kljuc: kawasaki; Vrednost: motor3
If you want to delete duplicate items, you can use function array_unique().
Example:
<?php
$motorcycle_prices = array(3500, 3700, 2300, 3500, 3500);
print_r($motorcycle_prices);
$motorcycle_prices = array_unique($motorcycle_prices);
print_r($motorcycle_prices);
?>
Result:
Array ( [0] => 3500 [1] => 3700 [2] => 2300 [3] => 3500 [4] => 3500 ) Array ( [0] => 3500 [1] => 3700 [2] => 2300 )
If you want to sum up all the values that are in array, you can use function array_sum().
Example 2:
<?php
$motorcycle_prices = array(3500, 3700, 2300, 3500, 3500);
echo "Average price = ",
array_sum($motorcycle_prices) / count($motorcycle_prices);
?>
Result:
Average price = 3300
Explanation: we found the average price for motorcycles. Motorcycle prices are in array $motorcycle_prices.
Function array_flip() changes the places of keys and values.
Example 3:
<?php
$japan = array("motorcycle1" => "honda", "motorcycle2" => "suzuki",
"motorcycle3" => "kawasaki");
foreach ($japan as $key => $value) {
echo "Key: $key; Value: $value<br />";
}
echo "<br />";
$japan = array_flip($japan);
foreach ($japan as $key => $value) {
echo "Key: $key; Value: $value<br />";
}
?>
Result:
Key: motorcycle1; Value: honda
Key: motorcycle2; Value: suzuki
Key: motorcycle3; Value: kawasaki
Key: honda; Value: motorcycle1
Key: suzuki; Value: motorcycle2
Key: kawasaki; Value: motorcycle3
Primer:
<?php
$cene_motora = array(3500, 3700, 2300, 3500, 3500);
print_r($cene_motora);
$cene = array_unique($cene_motora);
print_r($cene_motora);
?>
Rezultat:
Array ( [0] => 3500 [1] => 3700 [2] => 2300 [3] => 3500 [4] => 3500 ) Array ( [0] => 3500 [1] => 3700 [2] => 2300 )
Ako zelite da saberete sve vrednosti koje se nalaze u nizu, koristite funkciju array_sum().
Primer 2:
<?php
$cene_motora = array(3500, 3700, 2300, 3500, 3500);
echo "Prosecna cena = ", array_sum($cene_motora) / count($cene_motora);
?>
Rezultat:
Prosecna cena = 3300
Objasnjenje: pronasli smo srednju cenu motora. Cene motora se nalaze u nizu $cene_motora.
Funkcija array_flip() menja mesta kljucevima i vrednostima.
Primer 3:
<?php
$japan = array("motor1" => "honda", "motor2" => "suzuki",
"motor3" => "kawasaki");
foreach ($japan as $kljuc => $vrednost) {
echo "Kljuc: $kljuc; Vrednost: $vrednost<br />";
}
echo "<br />";
$japan = array_flip($japan);
foreach ($japan as $kljuc => $vrednost) {
echo "Kljuc: $kljuc; Vrednost: $vrednost<br />";
}
?>
Rezultat:
Kljuc: motor1; Vrednost: honda
Kljuc: motor2; Vrednost: suzuki
Kljuc: motor3; Vrednost: kawasaki
Kljuc: honda; Vrednost: motor1
Kljuc: suzuki; Vrednost: motor2
Kljuc: kawasaki; Vrednost: motor3
If you want to delete duplicate items, you can use function array_unique().
Example:
<?php
$motorcycle_prices = array(3500, 3700, 2300, 3500, 3500);
print_r($motorcycle_prices);
$motorcycle_prices = array_unique($motorcycle_prices);
print_r($motorcycle_prices);
?>
Result:
Array ( [0] => 3500 [1] => 3700 [2] => 2300 [3] => 3500 [4] => 3500 ) Array ( [0] => 3500 [1] => 3700 [2] => 2300 )
If you want to sum up all the values that are in array, you can use function array_sum().
Example 2:
<?php
$motorcycle_prices = array(3500, 3700, 2300, 3500, 3500);
echo "Average price = ",
array_sum($motorcycle_prices) / count($motorcycle_prices);
?>
Result:
Average price = 3300
Explanation: we found the average price for motorcycles. Motorcycle prices are in array $motorcycle_prices.
Function array_flip() changes the places of keys and values.
Example 3:
<?php
$japan = array("motorcycle1" => "honda", "motorcycle2" => "suzuki",
"motorcycle3" => "kawasaki");
foreach ($japan as $key => $value) {
echo "Key: $key; Value: $value<br />";
}
echo "<br />";
$japan = array_flip($japan);
foreach ($japan as $key => $value) {
echo "Key: $key; Value: $value<br />";
}
?>
Result:
Key: motorcycle1; Value: honda
Key: motorcycle2; Value: suzuki
Key: motorcycle3; Value: kawasaki
Key: honda; Value: motorcycle1
Key: suzuki; Value: motorcycle2
Key: kawasaki; Value: motorcycle3
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.