Re: json_encode confusion
| From: | DiRaven | Date: | Thu, 10 Nov 2011 15:08:35 +0000 |
| Subject: | Re: json_encode confusion | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-315625@lists.php.net to get a copy of this message | ||
On 11/10/2011 05:45 PM, Bastien Koert wrote:
Can someone point me in the correct direction? It may be that I need to manually create the key names as an array first, which I was hoping to avoid since the file format coming from the client is still in some flux. <?php//$data = file("inline_Nov_8_2011.txt"); $data = array('Legal Last Name|Legal Middle Initial|Legal First Name|Maiden/Other Name|Social Insurance No.|Date of Birth|Gender',
'Andros||Marisa||123456789|2/1/1988|Female');$json = ''; if(count($data)==2){
$keys = explode("|", $data[0]);
$fields = explode("|", $data[1]);
$combine = array_combine($keys, $fields);
$json = json_encode($combine);} echo $json; ?> That's the code I've just used. And it gave me perfect output: {"Legal Last Name":"Andros","Legal Middle Initial":"","Legal First Name":"Marisa","Maiden\/Other Name":"","Social Insurance No.":"123456789","Date of Birth":"2\/1\/1988","Gender":"Female"} What version of the PHP do you use? -- Best regards, DiRaven