I have encode a php array with json_encode and I store it into db, when I was trying to decode it into a php array I get null.
this is my php array :
'Etablissment' => array(
'id' => '79',
'telephone' => '0620000000',
'nom' => 'RESTo',
'nbmaxpersonnes' => '20',
'hoursreservation' => '{"Lundi":{"matin":{"h_debut":"0630 ","h_fin":"0830 "},"midi":{"h_debut":"0730 ","h_fin":"1300 "}},"Mardi":{"matin":{"h_debut":"0700 ","h_fin":"0500 "},"midi":{"h_debut":"1530 ","h_fin":"1400 "}}}'
),
I want to decode the hoursreservation field so I do json_decode($etablissement['Etablissment']['hoursreservation']), I work with cakephp.
the following solution not working for me:
php json decode
and
json decode in php
asked Dec 27, 2016 at 9:16
Malki Mohamed
1,7182 gold badges25 silver badges42 bronze badges
2 Answers 2
<?php
$etablissement = [
'Etablissment' => array(
'id' => '79',
'telephone' => '0620000000',
'nom' => 'RESTo',
'nbmaxpersonnes' => '20',
'hoursreservation' => '{"Lundi":{"matin":{"h_debut":"0630 ","h_fin":"0830 "},"midi":{"h_debut":"0730 ","h_fin":"1300 "}},"Mardi":{"matin":{"h_debut":"0700 ","h_fin":"0500 "},"midi":{"h_debut":"1530 ","h_fin":"1400 "}}}'
)];
$d = json_decode($etablissement['Etablissment']['hoursreservation'], true);
$e = json_last_error();
var_dump($d);
answered Dec 27, 2016 at 9:22
M A SIDDIQUI
2,2251 gold badge21 silver badges24 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
Try
$etablissement = array('Etablissment' => array(
'id' => '79',
'telephone' => '0620000000',
'nom' => 'RESTo',
'nbmaxpersonnes' => '20',
'hoursreservation' => '{"Lundi":{"matin":{"h_debut":"0630 ","h_fin":"0830 "},"midi":{"h_debut":"0730 ","h_fin":"1300 "}},"Mardi":{"matin":{"h_debut":"0700 ","h_fin":"0500 "},"midi":{"h_debut":"1530 ","h_fin":"1400 "}}}'
));
echo json_decode($etablissement['Etablissment']['hoursreservation'],true);
You will get array, there is not problem with it.
answered Dec 27, 2016 at 9:21
Amruth
5,9122 gold badges30 silver badges41 bronze badges
Comments
lang-php
json_decode($array['Etablissment']['hoursreservation']);(with the array you provided) and it works fine (on my end). Maybe your array isn't a 'array'.