3

i am getting problem to decode this string in to array

$json_array = json_decode($_POST['time_array'],true);

        for ($i = 0; $i < count($json_array); $i++)
        {
            $day = $json_array->day; 
            $start_time = $json_array->start_time;
            $end_time = $json_array->end_time;

            $insert_time="INSERT INTO `nesbaty_working_time` (`provider_id`,
                                              `day`,
                                              `opening_time`, 
                                              `closing_time`, 
                                              `time`, 
                                              `status`) 
                                              VALUES ('".$provider_id."',
                                              '".$day."', 
                                              '".$start_time."',  
                                              '".$end_time."',  
                                              '".$date."',
                                              '".$status."')";
            mysqli_query($con, $insert_time);

        }

The error is to get data from array

 Trying to get property of non-object 

and my array string is

[{"day":"Monday","start_time":"12 : 00 PM","end_time":"12 : 30 PM"},{"day":"Tuesday","start_time":"12 : 00 PM","end_time":"12 : 30 PM"}]
1
  • what does var_dump($json_array) says? Commented May 17, 2018 at 5:46

2 Answers 2

4

If you want object type return then remove true from json_decode like

$json_array = json_decode($_POST['time_array']);

and use index to access your returned data like

$day = $json_array[$i]->day; 
$start_time = $json_array[$i]->start_time;
$end_time = $json_array[$i]->end_time;
Sign up to request clarification or add additional context in comments.

Comments

2

Your JSON is an multi-dimensional array. You have to access it with index as follows

$json_array[$i]['day'];
$json_array[$i]['start_time'];

Also note

json_decode($_POST['time_array'],true); // returns an associative array 

json_decode($_POST['time_array']); // returns an object

Reference Link

1 Comment

please write in brief.@SAMIR

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.