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>