0

I have this multi dimension array in JSON format.

{
     "CUSTOMER_ORDER": 
        [
         {
         "customer_number": "51",
                "table_no": "7",
                "menu_name": "Fried Chicken",
                "menu_quatity": "5",
                "menu_price": "200",
                "order_total_price": "1000"
         },
         {
           "customer_number": "51",
           "table_no": "7",
           "menu_name": "Fries",
           "menu_quatity": "5",
           "menu_price": "200",
           "order_total_price": "1000"
         }
        ]
    }

My question is that how do I get specific data on index 0 of CUSTOMER_ORDER?

For example I want to get the menu_name which is Fried Chicken, Thanks.

Update

I am having trouble after decoding the JSON data, what I get is this:

json_decode() expects parameter 1 to be string, array given

when I used this:

$json_data = $this->post('CUSTOMER_ORDER');
$json_decoded = json_decode($json_data);
$customer_nickname = $json_decoded->CUSTOMER_ORDER[0]->customer_nickname;

and this when I try this:

 Undefined index: CUSTOMER_ORDER

$json_decoded = json_decode($json_data,true);
or
$json_decoded = json_decode(json_encode($json_data),true);

$customer_nickname = $json_decoded['CUSTOMER_ORDER'][0]['customer_nickname'];

By the way I am using CodeIgniter 3. I hope someone help me. Thanks.

Armali
  • 18,255
  • 14
  • 57
  • 171
ropenrom24
  • 517
  • 8
  • 18

1 Answers1

1

I hope it helps.

 $json = '{
         "CUSTOMER_ORDER": 
            [
             {
             "customer_number": "51",
                    "table_no": "7",
                    "menu_name": "Fried Chicken",
                    "menu_quatity": "5",
                    "menu_price": "200",
                    "order_total_price": "1000"
             },
             {
               "customer_number": "51",
               "table_no": "7",
               "menu_name": "Fries",
               "menu_quatity": "5",
               "menu_price": "200",
               "order_total_price": "1000"
             }
            ]
        }';

        $jsonDecodedArray = json_decode($json);
    print_r($jsonDecodedArray->CUSTOMER_ORDER[0]->customer_number);

    // OUTPUT 51