php - getting the sum of values from multidimensional array -
i need in getting sum of values of array, tried research on how solved end getting errors in using array_sum() end in array_sum expects 1 parameter array, , tried $value3+=$value3 , having result not correct, way here code:
var_export($_post['guests']); echo "<br />"; foreach($_post['guests'] $key1 => $value1){ foreach($value1 $key2 => $value2){ foreach($value2 $value3){ } } echo "room type: " . $key1 . " no. of rooms: " . $key2 . " no. of guest: " . array_sum($value3) . "<br /> "; } and here result of this:
array ( 1 => array ( 2 => array ( 0 => '1', 1 => '2', ), ), 2 => array ( 1 => array ( 0 => '4', ), ), ) warning: array_sum() expects parameter 1 array, string given in c:\xampp\htdocs\nation\reservation-form3.php on line 14 room type: 1 no. of rooms: 2 no. of guest:
warning: array_sum() expects parameter 1 array, string given in c:\xampp\htdocs\nation\reservation-form3.php on line 14 room type: 2 no. of rooms: 1 no. of guest:
i think want so
$_post['guests'] = array ( // roomtype => ( roomnum => ( guestlist // though don't understand why guests in room array // no. of room can't 0 :) 1 => array ( 2 => array ( 0 => '1', 1 => '2', ), ), 2 => array ( 1 => array ( 0 => '4', ), ), ); echo "<br />"; foreach($_post['guests'] $key1 => $value1) // loop roomtype foreach($value1 $key2 => $value2) // loop roomnum echo "room type: " . $key1 . " no. of rooms: " . $key2 . " no. of guest: " . array_sum($value2) . "<br />"; result
<br /> room type: 1 no. of rooms: 2 no. of guest: 3<br /> room type: 2 no. of rooms: 1 no. of guest: 4<br />
Comments
Post a Comment