Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am currently tyring to take a json object and convert it into a php array for database storage. It was working initially but for some reason or change, now all of the sudden it stopped working. $data holds the json object. I am then using json_decode for conversion. It returns nothing, in other words it is not an array. How can I convert properly a json object into a php array? DEMO

    if(isset($_POST['submit'])) {

       $data = stripslashes($_POST['data']);
       $personArray = json_decode($data, true);
       print_r($personArray);

        if (is_array($personArray))
        {

    foreach($personArray as $key => $value){
      $person_id = $value['personID'];
      $main1 = ($value['main1']  == "true") ? 1 : 0;
      $main2 = ($value['main2'] == "true") ? 1 : 0;
      $person_fname = $value['firstName'];
      $person_lname = $value['lastName'];
      $person_phone = $value['person_phone'];
      $person_phone_alt = $value['person_phone_alt'];
      $person_status = "ACTIVE";

      $query_init2 = "INSERT INTO person (main1, main2, first_name, last_name, person_phone, person_phone_alt) VALUES (:main1, :main2, :first_name, :last_name, :person_phone, :person_phone_alt) ON DUPLICATE KEY UPDATE main1=:main12, main2=:main22, first_name=:first_name2, last_name=:last_name2, person_phone=:person_phone2, person_phone_alt=:person_phone_alt2";
      $query_prep2 = $db_con->prepare($query_init2);
      $insert_result2 = $query_prep2->execute(array(
        ":person_id" => $person_id, 
        ":main1" => $main1,
        ":main2" => $main2,
        ":first_name" => $person_fname,
        ":last_name" => $person_lname,
        ":person_phone" => $person_phone,
        ":person_phone_alt" => $person_phone_alt,
        ":main12" => $main1,
        ":main22" => $main2,
        ":first_name2" => $person_fname,
        ":last_name2" => $person_lname,
        ":person_phone2" => $person_phone,
        ":person_phone_alt2" => $person_phone_alt
      ));


    }

    }else{
        echo "Not an array";
    }


    }

HTML

<textarea name="data">
[
  {
    "firstName": "Danny",
    "lastName": "LaRusso",
    "ciscoID": "123",
    "email": "[email protected]",
    "phone": "(555) 121-2121",
    "fax": "(123) 123-4567",
    "contact_role": true,
    "netacadContact": true,
    "netacadStaff": false,
    "netacadSuccess": false,
    "instructor_role": false
  },
  {
    "firstName": "Sensei",
    "lastName": "Miyagi",
    "ciscoID": "456",
    "email": "[email protected]",
    "phone": "(555) 444-2222",
    "fax": "(123) 123-4567",
    "contact_role": false,
    "netacadContact": false,
    "netacadStaff": false,
    "netacadSuccess": false,
    "instructor_role": true
  }
]
</textarea>
share|improve this question

3 Answers 3

up vote 2 down vote accepted

It depends where you place stripslashes. Try placing the stripslashes as you are about to decode the json object into a php array like this:

$data = $_POST['data'];
$personArray = json_decode(stripslashes($data), true);
var_dump($personArray);
if (is_array($personArray)){
    --rest of code
}
share|improve this answer
    
This works perfect! –  Code_Ed_Student Mar 2 at 17:09

Your json data looks fine for me and the json_decode also. Looks like some server configuration mixin up the stripslashes($_POST['data']) line and corrupt your json data.

Try to output $data to determine, what happend with your json data.

share|improve this answer
    
Yes, I try doing var_dump and it turned out to be an empty string. –  Code_Ed_Student Mar 2 at 11:35

Put quotes around the keys in your JSON string. for example: (I see you edited your original post and added the quotes)

"firstName": "Danny",
"lastName": "LaRusso",

Second thing, your array contains objects, you are trying to access those properties in array notation, you need to use '->'.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.