Calculating from or inside a multidimensional array

0 Benny Lindwall · August 15, 2015
Hi

I'm trying to get a total from a multidimensional array.
The array is as follows when submitted using "print_r" in <pre> tags

Array
{
[0] => Array
{
[name] => Name 1
[amount] => 2
[price] => 5
}
[1] Array
{
[name] => Name 2
[price] => 2
[price] => 5
}
}

I've put the code in if statements to get the array like above, code here:
$total = 0;
$items = array();
if(!empty($_POST['it1'])) {
array_push($items, array(
'name' => 'Name 1',
'amount' => $_POST['it1'],
'price' => '5'
));
// total calc here
$total = $total + ();
}
if(!empty($_POST['it2'])) {
array_push($items, array(
'name' => 'Name 2',
'amount' => $_POST['it2'],
'price' => '5'
));
// total calc here
$total = $total + ();
}
// more if statements below that looks the same as above with only a different number to the $_POST['it']



All I need now is to add the $total calculation in each if statement, problem is, I don't know how to get the values in the arrays.
I've googled it but can't find anything that helps

Also, is it a good idea, if at all possible, to save the entire $items array in a mysql field called items set to longtext to can easely get all the information in it later on?

Thanks in advanced

Regards

Benny

Post a Reply

Replies

Oldest  Newest  Rating
0 jethro daniel · August 16, 2015
you can use a foreach loop
  • 1

PHP

107,175 followers
About

Server-side, HTML embedded scripting language used to create dynamic Web pages.

Links
Moderators