Re: Stripping a JSON of extra characters in PHP

Arno Welzel <[email protected]> Thu, 9 Feb 2023 20:22:42 +0100
Newsgroups comp.lang.php
Message-ID <[email protected]>
The Doctor, 2023-02-07 17:10:

> In article <[email protected]>,
> Arno Welzel  <[email protected]> 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