Jayvirsinh Vaghela
Jayvirsinh Vaghela

Reputation: 157

trying to get property of non-object in php array

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"}]

Upvotes: 3

Views: 1387

Answers (2)

R B
R B

Reputation: 658

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;

Upvotes: 4

Samir Selia
Samir Selia

Reputation: 7065

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

Upvotes: 2

Related Questions