Sunday 15 April 2012

php - How to parse JSON Array with different keys on the same level -



php - How to parse JSON Array with different keys on the same level -

i have array , need summ of quantity * price. array keys on 3rd level different.

i utilize php code can take cost & quantity first item ['506-p1-8_p2-_p3-']

$quantity = $elements['1af7e792-bcff-4a6c-9bdb-dd5023b0251a']['items']['506-p1-8_p2-_p3-'][quantity]; $price = $elements['1af7e792-bcff-4a6c-9bdb-dd5023b0251a']['items']['506-p1-8_p2-_p3-'][price]; $summ = $price*$quantity;

how parse levels summ = (price * quantity) give thanks can help me

array ( [1af7e792-bcff-4a6c-9bdb-dd5023b0251a] => array ( [is_advance] => 1 [items] => array ( [506-p1-8_p2-_p3-] => array ( [hash] => 506-p1-8_p2-_p3- [sku] => 501 [itemid] => 506 [quantity] => 6 [price] => 80.75 [currency] => uah [pricedesc] => [priceparams] => array ( [u0420u0430u0437u043cu0435u0440] => 8 ) [name] => qwerty ) [498-p1-6_p2-_p3-] => array ( [hash] => 498-p1-6_p2-_p3- [sku] => 498 [itemid] => 498 [quantity] => 5 [price] => 500 [currency] => uah [pricedesc] => [priceparams] => array ( [u0420u0430u0437u043cu0435u0440] => 6 ) [name] => qwerty ) ) ) )

it's hard tell how array generated in first place. going off of code you've posted, should sum total every item contained in array.

$items = $elements['1af7e792-bcff-4a6c-9bdb-dd5023b0251a']['items']; $sum = 0; foreach($items $item) { $sum += $item['quantity'] * $item['price']; }

php arrays json

No comments:

Post a Comment