简体   繁体   中英

json getting the right data on php

How do I get the output of Apple to be fruit and Beef to be meat with this kind of JSON? Is it even possible to get that data?

PS each array's first index will always be 1.

Example of desired output:

  1. Apple -> fruit
  2. Beef -> meat
  3. Celery -> vegetable
{
    "Apple": {
        "fruit": "1"
    },
    "Beef": {
        "meat" : "1"
    },
    "Celery": {
        "vegetable" : "1"
    }
}

You can use json_decode :

$obj = json_decode( $jsonString );
echo $obj->Apple->fruit;

et voila!

PS: Your above JSON is unvalid (maybe a typo): please add a : after "Celery"

PS2: each array's first index is not 1 , index(es) are 'Apple', 'Beef' and 'Celery'.

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