0

Am getting json object and receiving in php using file_get_contents().

$getAddEntryData = json_decode(file_get_contents("php://input"));

The problem is i have to concatenate php variable with received object.

If it's a php REQUEST object we are doing like this.

$timingsStatus = $_REQUEST['timings_'.$dayArray[$i].'_status'];

But how can i do with json decoded object.

i tried something like this ->.

$timingsStatus = $getAddEntryData->timings_.$dayArray[$i]._status;

Its not working, how can concat like this?.

Here is my structure of json data.

let data = {
        timings_enabled : this.form_data.map_timings,

        timings_mon_status : this.form_data.map_timings_mon_status,
        timings_mon_from : this.form_data.map_timings_mon_from,
        timings_mon_to : this.form_data.map_timings_mon_to,


        timings_tue_status : this.form_data.map_timings_tue_status,
        timings_tue_from : this.form_data.map_timings_tue_from,
        timings_tue_to : this.form_data.map_timings_tue_to,


        timings_wed_status : this.form_data.map_timings_wed_status,
        timings_wed_from : this.form_data.map_timings_wed_from,
        timings_wed_to : this.form_data.map_timings_wed_to,


        timings_thu_status : this.form_data.map_timings_thu_status,
        timings_thu_from : this.form_data.map_timings_thu_from,
        timings_thu_to : this.form_data.map_timings_thu_to,


        timings_fri_status : this.form_data.map_timings_fri_status,
        timings_fri_from : this.form_data.map_timings_fri_from,
        timings_fri_to : this.form_data.map_timings_fri_to,


        timings_sat_status : this.form_data.map_timings_sat_status,
        timings_sat_from : this.form_data.map_timings_sat_from,
        timings_sat_to : this.form_data.map_timings_sat_to,

        timings_sun_status : this.form_data.map_timings_sun_status,
        timings_sun_from : this.form_data.map_timings_sun_from,
        timings_sun_to : this.form_data.map_timings_sun_to

    }

PHP code what am trying to do.

$getAddEntryData = json_decode(file_get_contents("php://input"));
$dayArray = array('mon', 'tue', 'wed', 'thu', 'fri', 'sat', 'sun');

if (isset($getAddEntryData->timings_enabled)) {

    for ($i = 0; $i < count($dayArray); $i++) {

        //$timingsStatus = $_REQUEST['timings_'.$dayArray[$i].'_status'];
        $timingsStatus = $getAddEntryData->timings_.$dayArray[$i]._status;

        $source = str_replace('[[TIMINGS-STATUS]]', "Available", $source);

        if ($timingsStatus == 'Open') {

           // $timingsFromTime = $_REQUEST['timings_'.$dayArray[$i].'_from'];
            $timingsStatus = $getAddEntryData->timings_.$dayArray[$i]._from;


           // $timingsToTime = $_REQUEST['timings_'.$dayArray[$i].'_to'];
            $timingsStatus = $getAddEntryData->timings_.$dayArray[$i]._to;

            $source = str_replace('[[TIMINGS-'.strtoupper($dayArray[$i]).'-STATUS]]', $timingsStatus, $source);
            $source = str_replace('[[TIMINGS-'.strtoupper($dayArray[$i]).'-FROM]]', $timingsFromTime, $source);
            $source = str_replace('[[TIMINGS-'.strtoupper($dayArray[$i]).'-TO]]', $timingsToTime, $source);

        } else {

            $source = str_replace('[[TIMINGS-'.strtoupper($dayArray[$i]).'-STATUS]]', $timingsStatus, $source);
            $source = str_replace('[[TIMINGS-'.strtoupper($dayArray[$i]).'-FROM]]', "", $source);
            $source = str_replace('[[TIMINGS-'.strtoupper($dayArray[$i]).'-TO]]', "", $source);

        }

    }

}
3
  • can you include the structure of your decoded json Commented Dec 5, 2016 at 6:19
  • @NewbeeDev edited question Commented Dec 5, 2016 at 6:26
  • Seems your json is not valid... Commented Dec 5, 2016 at 6:28

3 Answers 3

2

Try with -

$timingsStatus = $getAddEntryData->{'timings_' . $dayArray[$i] . '_status'};
Sign up to request clarification or add additional context in comments.

1 Comment

0

to do that just change your, this function will convert your json into an array

$getAddEntryData = json_decode(file_get_contents("php://input"));

to

$getAddEntryData = json_decode(file_get_contents("php://input"), true);

so this concatination works

$timingsStatus = $getAddEntryData['timings_'.$dayArray[$i].'_status'];

2 Comments

this and all i have done @Newbee only porblem with concat
@AppuAmruth did you tried and does it solve? yes. that's all you have to do.
-1

A "JSON decoded" object, is a simple object of PHP StdClass. That means you can simply use it accessing propreties (public). If you're not used to object, you can "decode" your JSON in an associative array. In this case, you add a second parameter to your json_decode(string, true)

This cannot work because PHP objects use the "arrow" -> operator to access properties/methods

$getAddEntryData->timings_->$dayArray[$i]->_status;

If your lost with your structure, I recommend you to always use "var_dump($variable)", one of the best help you can have there.

FYI, don't use $_REQUEST in PHP, but prefer POST, GET, SERVER... which are much better and always valid.

Comments

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.