Skip to content

Instantly share code, notes, and snippets.

@bangiqi
Last active January 30, 2018 13:57
Show Gist options
  • Save bangiqi/d883220a6afb71025f44faf613ec6a6c to your computer and use it in GitHub Desktop.
Save bangiqi/d883220a6afb71025f44faf613ec6a6c to your computer and use it in GitHub Desktop.
<?php
$data = [];
$data = [
[
"title"=>"PKS",
"total"=>100
],
[
"title"=>"Gerinda",
"total"=>30
]
];
foreach($data as $value){
$arraysum[] = $value["total"];
}
$hasilarraysum = array_sum($arraysum);
foreach($data as $value){
$hasil = $value["total"]/$hasilarraysum*100;
$hasilFormat = number_format($hasil);
echo "persentase partai ".$value["title"].": ".$hasilFormat."% <br>";
}
echo "<br>ini contoh lainnya <br>";
$jumCowok = 5;
$jumCewek = 7;
$total = $jumCowok + $jumCewek;
// menghitung prosentase laki-laki dan perempuan
$prosenCowok = $jumCowok/$total * 100;
$prosenCewek = $jumCewek/$total * 100;
$angka_cowok = number_format($prosenCowok);
$angka_cewek = number_format($prosenCewek);
echo "jumlah laki-laki : ".$angka_cowok."%<br>";
echo "jumlah perempuan : ".$angka_cewek."%<br>";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment