简体   繁体   中英

Is it possible to print the value of Key from the (key-value)pair of json object in laravel

How to print the 'key' value from the json object in laravel.

The json array:

 "pricing": {
"in": {
  "categories": [
    "ccTLD",
    "Geography"
  ],
  "addons": {
    "dns": true,
    "email": true,
    "idprotect": true
  },
  "group": "",
  "register": {
    "1": "704.39"
  },
  "transfer": {
    "1": "704.39"
  },
  "renew": {
    "1": "704.39"
  }
},

How to display "in" from the above data?

Put JSON in a variable. Ex. $jsonVariable .

json_decode($jsonVariable)->pricing->in

First of all i think there is some mistake in your json here is my funtion with two ways of achiving the same thing

public function demo()
    {
        $json_array = '
        {
                "pricing": {

                        "in": {
                        "categories": [
                                "ccTLD",
                                "Geography"
                            ],
                        "addons": {
                                "dns": true,
                                "email": true,
                                "idprotect": true
                            },
                            "group": "",
                            "register": {
                                "1": "704.39"
                            },
                            "transfer": {
                                "1": "704.39"
                            },
                            "renew": {
                                "1": "704.39"
                            }
                        }

                    }
             }
         ';
        echo "<pre>";
       print_r(json_decode($json_array)->pricing->in);
       $asso_array = json_decode($json_array, true);
        print_r($asso_array["pricing"]["in"]);
       exit;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM