0

Guys This is my array and I want to access the variable from the array which name is time_array[]

{"type":"updatebusiness","provider_id":"1","business_name":"uur","email_work":"[email protected]","phone_work":"8989889889","vat_no":"39r","address1":"gehjfmjhjfk","address2":"gehjfmjhjfk","postcode":"367383","latitude":"updatebusiness","longitude":"updatebusiness","time_array":[{"day":"Monday","start_time":"10 : 00 AM","end_time":"01 : 00 PM"},{"day":"Friday","start_time":"01 : 00 PM","end_time":"01 : 00 PM"}]}

and this my php code

$requestBody = file_get_contents('php://input');
$requestBody = json_decode($requestBody, TRUE);



            $time_array=$requestBody['time_array'];
            $json_array = json_decode($time_array,True);

            for ($i = 0; $i < sizeof($json_array); $i++)
            {
                $day = $json_array[$i]->day; 
                $start_time = $json_array[$i]->start_time;
                $end_time = $json_array[$i]->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);

so I am confused that how to access array inside the array

2
  • you haven't said what is the issue with your current code Commented May 21, 2018 at 6:44
  • issue is i cant ACCESS the array data which is inside array!! Commented May 21, 2018 at 6:48

2 Answers 2

1

You don't need to decode the $time_array variable again. Once you did:

$requestBody = json_decode($requestBody, TRUE);

You have access to the time_array like so:

$time_array = $requestBody['time_array'];

And than you can loop trough it like so:

foreach($time_array AS $time){
   $day = $time['day'];
   $start_time = $time['start_time'];
   $end_time = $time['end_time'];
   //Your insert query here
}
Sign up to request clarification or add additional context in comments.

Comments

0

1st time i have encoded data so whole array is converted in php string and the 2nd array which is inside is also converted in to string but now i have encoded it again then again decoded and it works..

            $time_array=$requestBody['time_array'];
            $abc=json_encode($time_array);
            $json_array = json_decode($abc);

            for ($i = 0; $i < sizeof($json_array); $i++)
            {


                $day = $json_array[$i]->day; 
                $start_time = $json_array[$i]->start_time;
                $end_time = $json_array[$i]->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);

            }

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.