How do I strip unnecessary [] ?
On Tue, 10 Jan 2023 01:17:47 -0000 (UTC), The Doctor wrote:
How do I strip unnecessary [] ?
I'd use array's `reduce()` for that. e.g.
var resultObj = theArray.reduce((res, obj) => Object.assign(res, obj), {});
$contact_details=[];[...]
$contact_details[] = array([...]
"contact_details"=>array(
'first_name' =>$_POST['bill_first_name'],
'last_name' => $_POST['bill_last_name'],
'email' => $_POST['email'],
'phone' => $_POST['bill_phone'],
)
);
$myObj = array_merge($storevalues,$cartarray,$contact_details,$billing_details);
$myJSON = json_encode($myObj);
Here is and example of what is generated:
[{"username":"demouser","password":"password","store_id":"store3","checkout_id":"chkt23NGFtore3","api_token":"yesguy","txn_total":"189.00","environment":"qa","action":"preload"},{"cart":[[{"items":{"url":"https:\/\/www.pdsolutions.ca\/images\/procircle.png","description":"Protecting Pollinators","product_code":"PP-1294","unit_cost":60,"quantity":"1"}},{"items":{"url":"https:\/\/www.pdsolutions.ca\/images\/procircle.png","description":"Agricultural Health Study","product_code":"AHS-1298","unit_cost":60,"quantity":"1"}},{"items":{"url":"https:\/\/www.pdsolutions.ca\/images\/procircle.png","description":"Pesticide Applicator Records","product_code":"PAR-1302","unit_cost":60,"quantity":"1"}}]]},{"subtotal":180,"tax":{"amount":9,"description":"GST","rate":"0.05"}},{"contact_details":{"first_name":"Dave","last_name":"Yadallee","email":"root@nk.ca","phone":"7804734587"}},{"billing_details":{"address_1":"","city":"","province":"","country":"CA","postal_code":""}}]
And here is what is being looked for
{
"store_id":"moneris",
[...]
}
How do I strip unnecessary [] ?
On Tue, 10 Jan 2023 01:17:47 -0000 (UTC), The Doctor wrote:
How do I strip unnecessary [] ?
I'd use array's `reduce()` for that. e.g.
var resultObj = theArray.reduce((res, obj) => Object.assign(res, obj), {});
[ [ 0, 1, 2 ], [ 'tutu' ] ]
Le 10/01/2023 |a 05:29, JJ a |-crit-a:
On Tue, 10 Jan 2023 01:17:47 -0000 (UTC), The Doctor wrote:
How do I strip unnecessary [] ?
I'd use array's `reduce()` for that. e.g.
var resultObj = theArray.reduce((res, obj) => Object.assign(res, obj), {});
const array = [
[0, 1, 2], [], ["tutu"], undefined
];
const result = array.filter(
(tab) => (tab && tab.length));
console.log(result);
[ [ 0, 1, 2 ], [ 'tutu' ] ]
you could improve filter callback to check that array members are arrays.
On Tue, 10 Jan 2023 01:17:47 -0000 (UTC), The Doctor wrote:--- Synchronet 3.21d-Linux NewsLink 1.2
How do I strip unnecessary [] ?I'd use array's `reduce()` for that. e.g.
var resultObj = theArray.reduce((res, obj) => Object.assign(res, obj), {});
In comp.lang.php The Doctor <doc...@doctor.nl2k.ab.ca> wrote:
$contact_details=[];[...]
$contact_details[] = array([...]
"contact_details"=>array(
'first_name' =>$_POST['bill_first_name'],
'last_name' => $_POST['bill_last_name'],
'email' => $_POST['email'],
'phone' => $_POST['bill_phone'],
)
);
$myObj = array_merge($storevalues,$cartarray,$contact_details,$billing_details);
$myJSON = json_encode($myObj);
Here is and example of what is generated:
[{"username":"demouser","password":"password","store_id":"store3","checkout_id":"chkt23NGFtore3","api_token":"yesguy","txn_total":"189.00","environment":"qa","action":"preload"},{"cart":[[{"items":{"url":"https:\/\/www.pdsolutions.ca\/images\/procircle.png","description":"Protecting Pollinators","product_code":"PP-1294","unit_cost":60,"quantity":"1"}},{"items":{"url":"https:\/\/www.pdsolutions.ca\/images\/procircle.png","description":"Agricultural Health Study","product_code":"AHS-1298","unit_cost":60,"quantity":"1"}},{"items":{"url":"https:\/\/www.pdsolutions.ca\/images\/procircle.png","description":"Pesticide Applicator Records","product_code":"PAR-1302","unit_cost":60,"quantity":"1"}}]]},{"subtotal":180,"tax":{"amount":9,"description":"GST","rate":"0.05"}},{"contact_details":{"first_name":"Dave","last_name":"Yadallee","email":"ro...@nk.ca","phone":"7804734587"}},{"billing_details":{"address_1":"","city":"","province":"","country":"CA","postal_code":""}}]
And here is what is being looked for
{
"store_id":"moneris",
[...]
}
How do I strip unnecessary [] ?First of all, you should strip unnecessary information from your
postings - in your case the content of $myObj and the output are the
only necessary information.
#v+
$a = array();
$b = array();
$a[] = ['contact_details' => ['y' => 'z']];
$b[] = ['billing_details' => ['y' => 'z']];
$c = array_merge($a, $b);
var_dump(json_encode($c));
#v-
string(59) "[{"contact_details":{"y":"z"}},{"billing_details":{"y":"z"}}]"
#v+
$a = ['contact_details' => ['y' => 'z']];
$b = ['billing_details' => ['y' => 'z']];
$c = array_merge($a, $b);
var_dump(json_encode($c));
#v-
string(55) "{"contact_details":{"y":"z"},"billing_details":{"y":"z"}}"
So there is no need to use reduce() or anything else. You are simply
using one (non-associative) level of arrays too much. This would be
easier to debug if you add a print_r($myObj); to your debugging
output.
Personally I'd prefer another version without the irritating--- Synchronet 3.21d-Linux NewsLink 1.2
array_merge for better readability, but this is just a matter of
taste producing the same array and the same output:
#v+
$a = ['y' => 'z'];
$b = ['y' => 'z'];
$c = ['contact_details' => $a, 'billing_details' => $b]; var_dump(json_encode($c));
#v-
Bye,
Stefan
--
http://kontaktinser.at/ - die kostenlose Kontaktboerse fuer Oesterreich Offizieller Erstbesucher(TM) von mmeike
Stefan: die s|+|fe Verf|+hrung!
(Sloganizer)
| Sysop: | Amessyroom |
|---|---|
| Location: | Fayetteville, NC |
| Users: | 65 |
| Nodes: | 6 (0 / 6) |
| Uptime: | 01:41:01 |
| Calls: | 862 |
| Files: | 1,311 |
| D/L today: |
10 files (20,373K bytes) |
| Messages: | 264,188 |