Re: Stripping a JSON of extra characters in PHP
[email protected] (The Doctor) Tue, 7 Feb 2023 16:10:55 -0000 (UTC)
| Newsgroups | comp.lang.php |
|---|---|
| Organization | NetKnow News |
| Message-ID | <[email protected]> |
In article <[email protected]>, Arno Welzel <[email protected]> wrote: >Arno Welzel, 2023-02-07 09:16: > >> The Doctor, 2023-02-07 02:28: >> >> [...] >>> cart is subarray with the main array, added items in the cart >>> is a subarray of the cart.> >>> Source >>> >>> https://developer.moneris.com/livedemo/checkout/preload_req/guide/dotnet >> >> No! Cart is an OBJECT there: >> >> "cart":{ >> ... >> } >> >> Also contact details: >> >> "contact_details":{ >> ... >> } >> >> The "{" indicates the beginning of an OBJECT while "[" starts an ARRAY. >> >> As I said: learn how to read JSON and try to understand how that format >> works! >> >> Also see: <https://www.json.org/json-en.html> > >In addition: > >To create an OBJECT in PHP use something like this - as a reduced example: > ><?php >class CartItem >{ > public $url; > public $description; > public $product_code; > public $unit_cost; > public $quantity; >} > >class TaxItem >{ > public $amount; > public $description; > public $rate; >} > >class Cart >{ > public $items; > public $subtotal; > public $tax; >}; > >$cart = new Cart(); > >$cartItem = new CartItem(); >$cartItem->url = "https:\/\/example.com\/examples\/item1.jpg"; >$cartItem->description = "One item"; >$cartItem->product_code = "one_item"; >$cartItem->unit_cost = "100.00"; >$cartItem->quantity = "1"; >$cart->items[] = $cartItem; > >$cartItem = new CartItem(); >$cartItem->url = "https:\/\/example.com\/examples\/item2.jpg"; >$cartItem->description = "Two item"; >$cartItem->product_code = "two_item"; >$cartItem->unit_cost = "200.00"; >$cartItem->quantity = "1"; >$cart->items[] = $cartItem; > >$cartItem = new CartItem(); >$cartItem->url = "https:\/\/example.com\/examples\/item3.jpg"; >$cartItem->description = "Three item"; >$cartItem->product_code = "three_item"; >$cartItem->unit_cost = "100.00"; >$cartItem->quantity = "1"; >$cart->items[] = $cartItem; > >$cart->subtotal = "400.00"; > >$taxItem = new TaxItem(); >$taxItem->amount = "52.00"; >$taxItem->description = "Taxes"; >$taxItem->rate = "13.00"; >$cart->tax = $taxItem; > >echo json_encode($cart, JSON_PRETTY_PRINT); >?> > >Result of this as formatted output: > >{ > "items": [ > { > "url": "https:\\\/\\\/example.com\\\/examples\\\/item1.jpg", > "description": "One item", > "product_code": "one_item", > "unit_cost": "100.00", > "quantity": "1" > }, > { > "url": "https:\\\/\\\/example.com\\\/examples\\\/item2.jpg", > "description": "Two item", > "product_code": "two_item", > "unit_cost": "200.00", > "quantity": "1" > }, > { > "url": "https:\\\/\\\/example.com\\\/examples\\\/item3.jpg", > "description": "Three item", > "product_code": "three_item", > "unit_cost": "100.00", > "quantity": "1" > } > ], > "subtotal": "400.00", > "tax": { > "amount": "52.00", > "description": "Taxes", > "rate": "13.00" > } >} > >Hopefully you understand the idea now - if you don't want an ARRAY in >the JSON then do not create one! If the specification asks for an OBJECT >then use an OBJECT based on a class with named members. > But The information is being pulled from a pre-filled hence you need, Something like //$items=array( if (isset($_POST["submit"])){ foreach ($_POST['quantity'] as $key => $value) { if( $value > 0){ if(isset($_POST['with_gst'][$key])){ $unit_cost = 63.00; }else{ $unit_cost = 60.00; //no gst included } $newsubtotal = ($unit_cost * $value); $quantity = ($value); $itemssubarray[]= array( 'url' => "https://www.pdsolutions.ca/images/newwhiteheader.png", 'description' => $_POST['description'][$key], 'product_code' => $_POST['id'][$key], 'unit_cost' => $unit_cost, 'quantity' => $quantity, ); } $subtotal = $subtotal+ $newsubtotal; $items_count++; } array_push($items, $itemssubarray); } //); $cartarray[] = array( "cart"=>array( $items ), 'subtotal' => $subtotal, "tax"=>array( 'amount' => ($subtotal * 0.05), 'description' => "GST", 'rate' => "5.00", ), ); >-- >Arno Welzel >https://arnowelzel.de > -- Member - Liberal International This is [email protected] Ici [email protected] Yahweh, King & country!Never Satan President Republic!Beware AntiChrist rising! Look at Psalms 14 and 53 on Atheism https://www.empire.kred/ROOTNK?t=94a1f39b Extremism is for those who love judging, and care nothing for accuracy. -unknown Beware https://mindspring.com