Re: Replace punctuation in an associative array
"J.O. Aho" <[email protected]> Sat, 4 Mar 2023 11:07:35 +0100
| Newsgroups | comp.lang.php |
|---|---|
| Message-ID | <[email protected]> |
On 3/4/23 06:19, The Doctor wrote: > Arno Welzel <[email protected]> wrote: > : The Doctor, 2023-03-02 13:44: > > : > I wish to replace [[ with [ > : > > : > AND > : > > : > ]] with ] in an associative array . > : > > : > How can I do this? > > : What do you mean with "in an associative array"? The values? The keys? > > : If you want to replace the characters in values: > > : <?php > : $array = [ > : 'value 1' => '[[something 1', > : 'value 2' => 'something 2]]', > : ]; > > : // Output array as it was before > : print_r($array); > > : foreach($array as $key => $value) > : { > : $array[$key] = str_replace( > : [ '[[', ']]' ], > : [ '[', ']' ], > : $value > : ); > : } > > : // Output as it was afterwards > : print_r($array); > > What is happening is that the arrauy is being formed by a subarray > and the end product is seeing a [[ at the start and a ]] > at the end when it should be seeing a [ at the start and a ] at the end. > > > You place your object in too many arrays, it had been better doing it the "right way" from the beginning, It's not that hard to create the json you want when using classes, in like 20mins I managed to generate this json from my objects: { "store_id": null, "api_token": null, "checkout_id": null, "txn_total": null, "environment": null, "action": null, "token": [ { "data_key": "1234", "issuer_id": "645sddfvdrt4tefd" }, { "data_key": "5678", "issuer_id": "645sddfvdrt4tefd" } ], "ask_cvv": null, "order_no": null, "cust_id": null, "dynamic_descriptor": null, "language": null, "recur": { "bill_now": null, "recur_amount": null, "start_date": null, "recur_unit": null, "recur_period": null, "number_of_recurs": null }, "cart": { "items": [ { "url": "https:\/\/example.net\/item?id=1", "description": "item 1", "product_code": "1", "unit_cost": "100", "quantity": "1" }, { "url": "https:\/\/example.net\/item?id=2", "description": "item 2", "product_code": "2", "unit_cost": "200", "quantity": "1" } ], "subtotal": null, "tax": { "amount": null, "description": null, "rate": null } }, "contact_details": { "first_name": null, "last_name": null, "email": null, "phone": null }, "shipping_details": { "address_1": null, "address_2": null, "city": null, "province": null, "country": null, "postal_code": null }, "billing_details": { "address_1": null, "address_2": null, "city": null, "province": null, "country": null, "postal_code": null } } I assumed that all values has to always be there, like an empty token list would be [] instead of null and so on, add two tokens and two items. using __construct(...) you could make it possible to input the values at once instead of how I done creating the object and then assign each variable one by one. You have the opportunity to improve things. https://pastebin.com/jnz1RYWj sadly Moneris seems to want everything to be as strings, if you could have used float/decimal/duble/int then you didn't have to convert between string and a numeric value back and forth. -- //Aho