0

Im trying to decode json and get value from the json code.

[{
    "restaurant_id":1,
    "menu_template_id":2,
    "add_food_item_a":1,
    "menu_category_id":1,
    "status":0
} ,
{
    "restaurant_id":1,
    "menu_template_id":2,
    "add_food_item_a":2,
    "menu_category_id":1,
    "status":0
}]

i need to read from this json and create an array with add_food_item_a and status.

currently I'm using like this

public function readJson()
    {
        $json_obj = json_decode('');
        if (isset($GLOBALS["HTTP_RAW_POST_DATA"]) && !empty($GLOBALS["HTTP_RAW_POST_DATA"])) {
            $json_text = $this->cleanMe($GLOBALS["HTTP_RAW_POST_DATA"]);
            // now insert into user table
            $json_obj = json_decode($json_text);
        }
        return $json_obj;
    }

and I call this function like

$add_food_item_a = isset($json_obj->add_food_item_a) ? $json_obj->add_food_item_a : '';

but can't read from this array of json code

4
  • have you tried using json_decode? Commented Jun 21, 2017 at 9:34
  • user json_decode($your_array,true); then use foreach loop to travel associative array & then you can create the any are with the available associative array paramaneter Commented Jun 21, 2017 at 9:37
  • first of this is in api, i can't get touch into it. Commented Jun 21, 2017 at 9:39
  • i just updated my question with the current status Commented Jun 21, 2017 at 9:44

1 Answer 1

1

Are you trying to fetch the value of 'add_food_item_a' and 'status'

    <?php

    $json='[{
        "restaurant_id":1,
        "menu_template_id":2,
        "add_food_item_a":1,
        "menu_category_id":1,
        "status":0
    } ,
    {
        "restaurant_id":1,
        "menu_template_id":2,
        "add_food_item_a":2,
        "menu_category_id":1,
        "status":0
    }]';

    echo "<pre>";
    $array = json_decode($json,1);
    print_r($array);
    foreach($array as $value)
    {
        echo "\n".$value['add_food_item_a'];
        echo "\n".$value['status'];
    }
Sign up to request clarification or add additional context in comments.

3 Comments

my json code is in postman, i need to retreve as an array thats my problem
json_decode($json_text,1); pass 1 as an parameter to json_decode you will get an array
but showing an error likeCannot use object of type stdClass as array

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.