Ads

Monday, 9 December 2013

How to convert JSON string to Array

Here I an showing convert JSON string to php Array

1. I have taken php Variable, I have assign JSON in that php Variable
2. Here is My JSON Array

 $jsonArray = '{"person_id":"BC21903E-9D4B-40E9-9DAD-00038273508F",  
     "last_seen_by":"8A40E73A-8228-4225-B980-A84D7148925A",  
     "last_name":"Last   175759",  
     "first_name":"First   175759",  
     "date_of_birth":"19710222",  
     "zip":"190133306",  
     "sex":"F",  
     "race":null,  
     "other_id_number":"NEW502828",  
     "language":"English",  
     "religion":null,  
     "ethnicity":"Black Or African American",  
     "last_appt_date":"20120726",  
     "allergies":[],  
     "diagnoses":[{  
       "person_id":"BC21903E-9D4B-40E9-9DAD-00038273508F",  
       "provider_id":"88E6E79C-9627-48C0-A2A2-9C1570396F5B",  
       "enterprise_id":"1",  
       "practice_id":"7",  
       "location_id":"EB95A675-65EC-4DBE-9112-D79EEFA3CC18",  
       "icd9cm_code_id":"726.2",  
       "description":"Impingement Syndrome Shoulder",  
       "date_diagnosed":""  
     }],  
     "medications":[{  
       "person_id":"BC21903E-9D4B-40E9-9DAD-00038273508F",  
       "provider_id":"88E6E79C-9627-48C0-A2A2-9C1570396F5B",  
       "enterprise_id":"1",  
       "practice_id":"7",  
       "location_id":"EB95A675-65EC-4DBE-9112-D79EEFA3CC18",  
       "medication_name":"nabumetone 750 mg tab",  
       "rx_quanity":"60",  
       "rx_refills":"0",  
       "date_last_refilled":"",  
       "sig_desc":"take 1 tablet (750MG) by oral route 2 times every day",  
       "start_date":"20120613",  
       "date_stopped":"20120613"  
     }]  
     }';  

3. I have used json_decode() function to convert JSON string to php Array

 $phpArray = json_decode($jsonArray, true);  
 echo "<pre>";  
 print_r($phpArray);  
 echo "</pre>";  

Result of JSON string to php Array

 Array  
 (  
   [person_id] => BC21903E-9D4B-40E9-9DAD-00038273508F  
   [last_seen_by] => 8A40E73A-8228-4225-B980-A84D7148925A  
   [last_name] => Last   175759  
   [first_name] => First   175759  
   [date_of_birth] => 19710222  
   [zip] => 190133306  
   [sex] => F  
   [race] =>   
   [other_id_number] => NEW502828  
   [language] => English  
   [religion] =>   
   [ethnicity] => Black Or African American  
   [last_appt_date] => 20120726  
   [allergies] => Array  
     (  
     )  
   [diagnoses] => Array  
     (  
       [0] => Array  
         (  
           [person_id] => BC21903E-9D4B-40E9-9DAD-00038273508F  
           [provider_id] => 88E6E79C-9627-48C0-A2A2-9C1570396F5B  
           [enterprise_id] => 1  
           [practice_id] => 7  
           [location_id] => EB95A675-65EC-4DBE-9112-D79EEFA3CC18  
           [icd9cm_code_id] => 726.2  
           [description] => Impingement Syndrome Shoulder  
           [date_diagnosed] =>   
         )  
     )  
   [medications] => Array  
     (  
       [0] => Array  
         (  
           [person_id] => BC21903E-9D4B-40E9-9DAD-00038273508F  
           [provider_id] => 88E6E79C-9627-48C0-A2A2-9C1570396F5B  
           [enterprise_id] => 1  
           [practice_id] => 7  
           [location_id] => EB95A675-65EC-4DBE-9112-D79EEFA3CC18  
           [medication_name] => nabumetone 750 mg tab  
           [rx_quanity] => 60  
           [rx_refills] => 0  
           [date_last_refilled] =>   
           [sig_desc] => take 1 tablet (750MG) by oral route 2 times every day  
           [start_date] => 20120613  
           [date_stopped] => 20120613  
         )  
     )  
 )  

No comments:

Post a Comment