Extracting value(s) from json array with php -
this question has answer here:
my cart store orders in json_encode array database. possible extract total price array.. if there 4 products want extract sum of 5 prices?
here example of array
{"73":{ "title":"test", "description":"", "quantity":1, "image":"", "price":90}, "66":{ "title":"title", "description":"", "quantity":1, "image":"", "price":80}, "shipping":{" quantity":1, "image":"", "description":"", "title":"free delivery", "price":0 } }
i want extract price
fields , shwo sum of them on page.
edit: array get
array ( [7] => array ( [title] => test [description] => test [quantity] => 1 [image] => [price] => 2 ) [6] => array ( [title] => test [description] => test [quantity] => 1 [image] => [price] => 12 ) [shipping] => array ( [quantity] => 1 [image] => [description] => [title] => [price] => 51 ) )
this json. needs converted array first.
and then, can use array function array_column() retrieve "price" values , apply array_sum() calculate total price on array.
// assuming $data json. $data = json_decode($data, true); $price = array_sum(array_column($data, "price"));
Comments
Post a Comment