Jump to content

calculate the price came from Json file and Array


tonton
Go to solution Solved by Barand,

Recommended Posts

Hi,

 
I have a json data, below which contains an item with tax:

   "date": "2015-05-05 12:41",
   "shop": "Toto",
   "products": [
      {
         "price": "2.00",
         "quantity": "1",
         "description": "chocolat",
         "tax": [
            {
               "price": "1.00",
               "quantity": "1",
               "description": "tax1"
            },
            {
               "price": "2.00",
               "quantity": "1",
               "description": "tax2"
            }
         ]
      }
   ]
}

I am able to calculate the price ($amountAll) with the following code:

$datajs = "myJsonSimple2.json";



$datajs = file_get_contents($datajs);

$decodageDatajs = json_decode($datajs);



$dateBiling = $decodageDatajs->date;
$shopName = $decodageDatajs->shop;

	foreach ($decodageDatajs->products as $obj)
		{
			$ItemPrice = $obj->price;			
			$ItemQuantity = $obj->quantity;
			$ItemDescription = $obj->description;
			
			
			
			$taxes = (array)$obj->tax;
			
			$taxesPrice1 = !empty($taxes[0]) ? $taxes[0]->price : 0.00;
			$taxesPrice2 = !empty($taxes[1]) ? $taxes[1]->price : 0.00; 
			
			$taxesQuantity1 = !empty($taxes[0]) ? $taxes[0]->quantity : 0;
			$taxesQuantity2 = !empty($taxes[1]) ? $taxes[1]->quantity : 0;
			
			$taxesDescription1 = !empty($taxes[0]) ? $taxes[0]->description : '1er tax null';
			$taxesDescription2 = !empty($taxes[1]) ? $taxes[1]->description : '2e tax null';
			
			
			$amountAll  = $ItemPrice * $ItemQuantity + $taxesPrice1 + $taxesPrice2;
			
		}

 But how I calculate if there are several items (2 or 3 or 5...)


{
   "date": "2015-05-05 12:41",
   "shop": "Toto",
   "products": [
      {
         "price": "2.00",
         "quantity": "1",
         "description": "chocolat",
         "tax": [
            {
               "price": "1.00",
               "quantity": "1",
               "description": "tax1"
            },
            {
               "price": "2.00",
               "quantity": "1",
               "description": "tax2"
            }
         ]
      },
      {
         "price": "3.00",
         "quantity": "1",
         "description": "bonbon",
         "tax": [
            {
               "price": "2.00",
               "quantity": "1",
               "description": "tax1"
            },
            {
               "price": "3.00",
               "quantity": "1",
               "description": "tax2"
            }
         ]
      },
      {
         "price": "2.00",
         "quantity": "1",
         "description": "gateaux",
         "tax": [
            {
               "price": "1.00",
               "quantity": "1",
               "description": "tax1"
            },
            {
               "price": "2.00",
               "quantity": "1",
               "description": "tax2"
            }
         ]
      }
   ]
} 
 
I need like as : 
$amountAll  = $ItemPrice1 * $ItemQuantity1   + 
			$ItemPrice2 * $ItemQuantity2 + 
			$ItemPrice3 * $ItemQuantity3  + 
			$taxesPrice1a + $taxesPrice2a  +
			 $taxesPrice1b + $taxesPrice2b  +
			 $taxesPrice1c + $taxesPrice2c; 
but this is wrong : 
 
I must counter and use while statement but I don,t know how I can write ?
$calculeItems = count($decodageDatajs->products);
$i = 0;
while ($i < $calculeItems)
	{



	foreach ($decodageDatajs->products as $obj)
		{
			$ItemPrice = $obj->price;			
			$ItemQuantity = $obj->quantity;
			$ItemDescription = $obj->description;
			
			
			
			$taxes = (array)$obj->tax;
			
			$taxesPrice1 = !empty($taxes[0]) ? $taxes[0]->price : 0.00;
			$taxesPrice2 = !empty($taxes[1]) ? $taxes[1]->price : 0.00; 
			
			$taxesQuantity1 = !empty($taxes[0]) ? $taxes[0]->quantity : 0;
			$taxesQuantity2 = !empty($taxes[1]) ? $taxes[1]->quantity : 0;
			
			$taxesDescription1 = !empty($taxes[0]) ? $taxes[0]->description : '1er tax null';
			$taxesDescription2 = !empty($taxes[1]) ? $taxes[1]->description : '2e tax null';
			
			
			$amountAll  = $ItemPrice * $ItemQuantity + $taxesPrice1 + $taxesPrice2;
			
/* this si wrong !!!!!			
$amountAll  = $ItemPrice1 * $ItemQuantity1   + 
			$ItemPrice2 * $ItemQuantity2 + 
			$ItemPrice3 * $ItemQuantity3  + 
			$taxesPrice1a + $taxesPrice2a  +
			 $taxesPrice1b + $taxesPrice2b  +
			 $taxesPrice1c + $taxesPrice2c;
			
*/			
			
		}
$i++;			
}
 
can you help me please
 
Thanks
 

 

 

 

 
 

 

 

 

 

Link to comment
Share on other sites

try

$data = json_decode($datajs, 1);  // decode as an array
echo '<pre>',print_r($data, true),'</pre>';

$amountAll = 0;
foreach ($data['products'] as $product) {
    foreach ($product['tax'] as $item) {
        $amountAll += $item['quantity'] * $item['price'];
    }
    
}
echo number_format($amountAll, 2);
Edited by Barand
Link to comment
Share on other sites

 

try

$data = json_decode($datajs, 1);  // decode as an array
echo '<pre>',print_r($data, true),'</pre>';

$amountAll = 0;
foreach ($data['products'] as $product) {
    foreach ($product['tax'] as $item) {
        $amountAll += $item['quantity'] * $item['price'];
    }
    
}
echo number_format($amountAll, 2);

Hi Brand,

 

I try it but I have a Fatal error afther Print_r

 

 

 

Fatal error: Cannot use object of type stdClass as array  ... on line 22

 

 

and here is line 22 :

foreach ($decodageDatajs['products'] as $product) {

What is wrong ?

 

Thanks

Link to comment
Share on other sites

Hi Barand, Many thanks :happy-04:

This great, it works.

But I change little bit, I don't know if It is correct or not.

 

What do you say ?

-------------------------------------------------------------------

price of chocolat 
2.00 and tax : 1.00 +2.00 = 5.00
 
Price of bonbon 3.00 with tax =
3.00 + 2.00 + 3.00 = 8.00
 
and gateaux = 2.00
2.00 + 1.00 +2.00 = 5.00
 
so
 
5.00+8.00+5.00 = 18.00
 
 
So 
 
$allOfPrice + $$amountAl = 18
-------------------------------------------------------------------
 
like this :
 
$amountAll = 0;
$price_product  = 0;

foreach ($data['products'] as $product) {
	
	$price_product+= $product['price'] * $product['quantity'];       // price without tax   New
	
	  }
  
foreach ($data['products'] as $product) {
	
	
    foreach ($product['tax'] as $item) {
        $amountAll += $item['quantity'] * $item['price'];
		//$amountAll += $item['quantity'] * $item['price'];  // with this is it double 
		
		
    }
    
}


$allOfPrice = $price_product;
echo number_format( $allOfPrice, 2) . '<br>'; //it get 7.00 amount without tax



echo number_format($amountAll, 2). '<br>'; // it get 11.00  all of tax

echo  number_format($allOfPrice + $amountAll, 2). '<br>';  // it get 18.00  price with tax  IT S FUNY

So what do you think : is it correct or I must change it ?

 

Thanks

Link to comment
Share on other sites

  • Solution

You don't need to loop twice

echo '<pre>';

$data = json_decode($datajs, 1);  // decode as an array
$amountAll = 0;
foreach ($data['products'] as $product) {
    $totalPrice = $product['price'];                 //get product price
    foreach ($product['tax'] as $tax) {
        $totalPrice += $tax['price'];                //add in the tax
    }
    printf('%-12s %5d x %8.2f<br>', $product['description'], $product['quantity'], $totalPrice);
    $amountAll += $product['quantity'] * $totalPrice;
}
printf('%-12s %5s   %8.2f<br>', '','Total', $amountAll);

echo '</pre>';

gives

chocolat         1 x     5.00
bonbon           1 x     8.00
gateaux          1 x     5.00
             Total      18.00
Edited by Barand
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.