Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.php > #19361

Re: Replace punctuation in an associative array

From "J.O. Aho" <user@example.net>
Newsgroups comp.lang.php
Subject Re: Replace punctuation in an associative array
Date 2023-03-03 10:36 +0100
Message-ID <k6dtc6FufhjU1@mid.individual.net> (permalink)
References <ttq5np$i9r$50@gallifrey.nk.ca> <k6bmapFcv37U1@mid.individual.net> <ttrh05$fo7$68@gallifrey.nk.ca>

Show all headers | View raw


On 3/3/23 02:03, The Doctor wrote:
> J.O. Aho <user@example.net> wrote:
> : On 3/2/23 13:44, The Doctor wrote:
> 
> : A wild guess, this still to do with your json, which you have asked help
> : with quite often.
> 
> : > I wish to replace [[ with [
> : >
> : > AND
> : >
> : > ]] with ] in an associative array .
> 
> : Don't store objects as an array and place those arrays in an array.
> 
> : Create an object and place the object into the array which is part of
> : your main-object.
> 
> All right.  Even in a recursive loop?

A recursive function should just return an array, but this array needs 
to be assigned to the objects array variable, not as a cell in that array.

_not_ like:
$object->array[] = recursiveLoopFunction($inputdate);

but like:
$object->array = recursiveLoopFunction($inputdate);


> : > How can I do this?
> 
> : If you just look at a string, string replace could work quite well to do
> : this
> : str_replace('[[','[',$str);
>  
> Woth a try.

I wouldn't say it's worth trying, it's tends to be better to do things 
the right way from the beginning. I do recommend you make classes that 
represents how your json should look like for example if you have:

{
	"order_id" : 12334,
	"total_tax": 1.10,
	"total_value": 100,
	"cart": [
		{
			"name": "item1",
			"quantity": 1,
			"price": 10
		},
		{
			"name": "item2",
			"quantity": 2,
			"price": 45
		},
	]
}

This would be two classes, the item class and the order class

class item {
	public string $name;
	public int $quantity;
	public float $price;
}

class order {
	public int $order_id;
	public float $total_tax;
	public float $total_value;
	public $cart = array();
}

here is an example code

<?php
// this is the input data we assume this is your POST data
$inputData = array("order_id" => 1234, "cart" => array( array("name" => 
"item1", "quantity" => 1, "price" => 10), array("name" => "item2", 
"quantity" => 2, "price" => 45) ) );

// this is the classes we have that describes the json
// we do init some values, just in case.
class item {
         public string $name;
         public int $quantity = 0;
         public float $price = 0.0;
}

class order {
         public int $order_id;
         public float $total_tax = 0.0;
         public float $total_value = 0.0;
         public $cart = array();
}


// here we assign the values from the post data
// without really verifying the data is okay
$order = new order();
$order->order_id = $inputData["order_id"];
$tax = 0.011; // an assumed tax % for this simplified example

// now we process the cart items
// we should take the price from a database
// instead of trusting a user
foreach($inputData["cart"] as $itemData) {
         $item = new item();
         $item->name = $itemData["name"];
         $item->quantity = $itemData["quantity"];
         $item->price = $itemData["price"];
	// we add the item to the cart array in the order object
         $order->cart[] = $item;
	// we store the totla_value in a local variable as we need it for the tax
         $total_value = $item->quantity * $item->price;
	// update the order total value/tax
         $order->total_value += $total_value;
         $order->total_tax += $tax * $total_value;
}

// the json should be the same as the one above the code, a zero missing,
// but should cause any issues as the value is equal.
echo json_encode($order, JSON_PRETTY_PRINT);
echo "\n"; // just a new line so that the prompt will come back below 
the json
?>

Don't forget that you can make functions that do parts of the moving 
data from the post data to the different objects, this way you make it 
easier to see what the code does and should make it easier to locate 
where a fault is without being distracted by code not related.

-- 
  //Aho

Back to comp.lang.php | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

Replace punctuation in an associative array The Doctor <doctor@doctor.nl2k.ab.ca> - 2023-03-02 12:44 +0000
  Re: Replace punctuation in an associative array "J.O. Aho" <user@example.net> - 2023-03-02 14:23 +0100
    Re: Replace punctuation in an associative array The Doctor <doctor@doctor.nl2k.ab.ca> - 2023-03-03 01:03 +0000
      Re: Replace punctuation in an associative array "J.O. Aho" <user@example.net> - 2023-03-03 10:36 +0100
    Re: Replace punctuation in an associative array Ezimene nimi Teine nimi <techfan55555@hotmail.com> - 2023-03-08 09:22 -0800
      Re: Replace punctuation in an associative array The Doctor <doctor@doctor.nl2k.ab.ca> - 2023-03-09 01:24 +0000
  Re: Replace punctuation in an associative array Arno Welzel <usenet@arnowelzel.de> - 2023-03-03 08:14 +0100
    Re: Replace punctuation in an associative array The Doctor <doctor@doctor.nl2k.ab.ca> - 2023-03-04 05:19 +0000
      Re: Replace punctuation in an associative array "J.O. Aho" <user@example.net> - 2023-03-04 11:07 +0100
      Re: Replace punctuation in an associative array Arno Welzel <usenet@arnowelzel.de> - 2023-03-04 14:42 +0100
        Re: Replace punctuation in an associative array The Doctor <doctor@doctor.nl2k.ab.ca> - 2023-03-04 14:57 +0000
          Re: Replace punctuation in an associative array "J.O. Aho" <user@example.net> - 2023-03-04 16:38 +0100
            Re: Replace punctuation in an associative array "J.O. Aho" <user@example.net> - 2023-03-05 00:41 +0100
              Re: Replace punctuation in an associative array The Doctor <doctor@doctor.nl2k.ab.ca> - 2023-03-04 23:58 +0000
                Re: Replace punctuation in an associative array "J.O. Aho" <user@example.net> - 2023-03-05 01:39 +0100
                Re: Replace punctuation in an associative array doctor@doctor.nl2k.ab.ca (The Doctor) - 2023-03-05 00:52 +0000
                Re: Replace punctuation in an associative array "J.O. Aho" <user@example.net> - 2023-03-05 12:16 +0100
                Re: Replace punctuation in an associative array The Doctor <doctor@doctor.nl2k.ab.ca> - 2023-03-05 14:08 +0000
                Re: Bureaucracy (Was: Replace punctuation in an associative array) "J.O. Aho" <user@example.net> - 2023-03-06 09:30 +0100
                Re: Replace punctuation in an associative array The Doctor <doctor@doctor.nl2k.ab.ca> - 2023-03-07 16:43 +0000
                Re: Replace punctuation in an associative array "J.O. Aho" <user@example.net> - 2023-03-07 18:56 +0100
                Re: Replace punctuation in an associative array The Doctor <doctor@doctor.nl2k.ab.ca> - 2023-03-07 22:53 +0000
                Re: Replace punctuation in an associative array Arno Welzel <usenet@arnowelzel.de> - 2023-03-08 23:44 +0100
                Re: Replace punctuation in an associative array The Doctor <doctor@doctor.nl2k.ab.ca> - 2023-03-09 01:25 +0000
                Re: Replace punctuation in an associative array Arno Welzel <usenet@arnowelzel.de> - 2023-03-10 09:08 +0100
                Re: Replace punctuation in an associative array The Doctor <doctor@doctor.nl2k.ab.ca> - 2023-03-10 15:20 +0000
                Re: Replace punctuation in an associative array doctor@doctor.nl2k.ab.ca (The Doctor) - 2023-04-01 14:08 +0000
                Re: Replace punctuation in an associative array doctor@doctor.nl2k.ab.ca (The Doctor) - 2023-04-06 00:26 +0000
                Re: Replace punctuation in an associative array doctor@doctor.nl2k.ab.ca (The Doctor) - 2023-04-12 22:45 +0000
                Re: Replace punctuation in an associative array "J.O. Aho" <user@example.net> - 2023-04-13 07:41 +0200
                Re: Replace punctuation in an associative array doctor@doctor.nl2k.ab.ca (The Doctor) - 2023-04-13 11:50 +0000
                Re: Replace punctuation in an associative array "J.O. Aho" <user@example.net> - 2023-04-13 14:26 +0200
                Re: Replace punctuation in an associative array doctor@doctor.nl2k.ab.ca (The Doctor) - 2023-04-13 22:37 +0000
                Re: Replace punctuation in an associative array doctor@doctor.nl2k.ab.ca (The Doctor) - 2023-04-14 01:12 +0000
                Re: Replace punctuation in an associative array "J.O. Aho" <user@example.net> - 2023-04-14 19:34 +0200
                Re: Replace punctuation in an associative array doctor@doctor.nl2k.ab.ca (The Doctor) - 2023-04-14 22:21 +0000
                Re: Replace punctuation in an associative array doctor@doctor.nl2k.ab.ca (The Doctor) - 2023-04-16 12:40 +0000
                Re: Replace punctuation in an associative array "J.O. Aho" <user@example.net> - 2023-04-16 22:03 +0200
  Re: Replace punctuation in an associative array Ezimene nimi Teine nimi <yyyyyeeeee00000@writeme.com> - 2023-03-06 03:23 -0800
    Re: Replace punctuation in an associative array The Doctor <doctor@doctor.nl2k.ab.ca> - 2023-03-06 16:21 +0000
  Re: Replace punctuation in an associative array V <vvvvvvvvvvvvvvvvvvvv11111@mail.ee> - 2023-04-03 06:55 -0700
    Re: Replace punctuation in an associative array doctor@doctor.nl2k.ab.ca (The Doctor) - 2023-04-03 14:18 +0000
  Re: Replace punctuation in an associative array Angel <heyeeeeeeeeeeeeeeeeeee@gmail.com> - 2023-04-15 05:33 -0700
    Re: Replace punctuation in an associative array doctor@doctor.nl2k.ab.ca (The Doctor) - 2023-04-15 13:52 +0000

csiph-web