Contoh sederhana kita memiliki orang yang membeli 3 jenis ikan, yang ada di dalam array multidimensi

$pesanan = array(
  array(
    "nama_barang" => "Ikan Teri",
    "harga" => "50000",
  ),
  array(
    "nama_barang" => "Ikan Asin",
    "harga" => "10500",
  ),
  array(
    "nama_barang" => "Ikan Lele",
    "harga" => "6300",
  )
);

lalu hitung total harga yang harus dibayar dengan melakukan looping (perulangan)

// nilai awal harus 0 ;) 
$harga_total = 0;
foreach($pesanan as $item=>$value)
{
// simpan nilai harga ke variabel $harga_total
$harga_total +=$value['harga'];
}
echo $harga_total;

Script lengkapnya

<?php
// array.php
$pesanan = array(
  array(
    "nama_barang" => "Ikan Teri",
    "harga" => "50000",
  ),
  array(
    "nama_barang" => "Ikan Asin",
    "harga" => "10500 ",
  ),
  array(
    "nama_barang" => "Ikan Lele",
    "harga" => " 6300",
  )
);
 
$harga_total = 0;
foreach($pesanan as $item=>$value)
{
$harga_total +=$value['harga'];
}
 
echo "Harga Total : " .  $harga_total;

hitung total harga array

Leave a comment

Your email address will not be published. Required fields are marked *