Re: Stripping [ ] from JSON arrays
[email protected] (Stefan Froehlich) 10 Jan 2023 08:10:09 GMT
| Newsgroups | comp.lang.php,comp.lang.javascript |
|---|---|
| Message-ID | <1t63bd197bi3c78e5n3e8%[email protected]> |
In comp.lang.php The Doctor <[email protected]> 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":"[email protected]","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 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üße Verführung! (Sloganizer)