Path: csiph.com!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail From: Arno Welzel Newsgroups: comp.lang.php Subject: Re: Stripping a JSON of extra characters in PHP Date: Thu, 9 Feb 2023 20:22:42 +0100 Lines: 79 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Trace: individual.net 9sF4Z4zKFWvdd/tuVRQZdwtqi/pA9pyB2RyWoUfMKuD+lPlnqS Cancel-Lock: sha1:D07pVOiyoYZLCYo/sj1AVowETUs= Content-Language: de-DE In-Reply-To: Xref: csiph.com comp.lang.php:19317 The Doctor, 2023-02-07 17:10: > In article , > Arno Welzel wrote: [...] >> 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 So what? You can also create objects based on "pre-filled" data. > //$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( CREATE AN OBJECT HERE! First define a class for this above all the code: class CartItem { public $url; public $description; public $product_code; public $unit_cost; public $quantity; } And then use THIS(!) to create the OBJECT(!) for it: $item = new Item(); $item->url = "https://www.pdsolutions.ca/images/newwhiteheader.png"; $item->description = $_POST['description'][$key]; $item->product_code = $_POST['id'][$key]; $item->unit_cost = $unit_cost; $item->quantity = $quantity; $itemssubarray[] = $item; [...] > $cartarray[] = array( > "cart"=>array( $items > ), NO! Create an OBJECT! > 'subtotal' => $subtotal, > "tax"=>array( > 'amount' => ($subtotal * 0.05), > 'description' => "GST", > 'rate' => "5.00", > ), > ); Sorry - but if you are really not able to understand what I write, you should pay someone to do the job for you. -- Arno Welzel https://arnowelzel.de