-3

I have this data in json file abc.json now, how to display keys and slso data (id & name) from this file and display them in php, but seperately. this is just example. i have lots of data.

       [{
            "Car": [{
                    "id": "1",
                    "name": "Bus"
                }, {
                    "id": "2",
                    "name": "Truck"
                }]
        },
        {
            "Fruit": [{
                    "id": "1",
                    "name": "Mango"
                }, {
                    "id": "2",
                    "name": "Pinapple"
                }]
        }]
9
  • you can either use curl or file_get_contents to achieve this .. Commented Oct 27, 2016 at 6:34
  • please help me with code.. i have googled.. but not found ...i am also new to json.. Commented Oct 27, 2016 at 6:36
  • i have a small request to the contributers of this community who are answering such questions, can we just give the hint to the users who are asking such questions instead of provinding them ready code and giving them solution and getting upvotes .. i guess if we follow this, it will ultimately imrove the users who are asking such questions ..i hate downvoting on this as it will demotivate the users hence i try to follow this principle.. what you say .. Commented Oct 27, 2016 at 6:40
  • @MittulAtTechnoBrave I can see that you're having a hard time getting upvotes, eh? :) Commented Oct 27, 2016 at 6:42
  • @RaxWeber well for me, its all about contibuting to the community by helping people instead of getting upvotes :) upvotes are just reward which you will eventually get if you help in a manner :) .. hope it makes sense. Commented Oct 27, 2016 at 6:43

2 Answers 2

3

Just get the contents of the file, decode it, then print:

<?php
    $content = json_decode(file_get_contents("abc.json"), true);
    print_r($content);
?>
Sign up to request clarification or add additional context in comments.

2 Comments

no.. not like that .. i am trying to show them in a list,,, but orint_r shows them in raw format.. like total array structure.. i dont need this..
@SurajRoy yes from here, you can use for loop to show data either in your table or whatever html structure you have .. for example check this out - php.net/manual/en/control-structures.foreach.php will give you better idea ..
0

Not tested btw but this should give you some ideas

/* Read the file and assign to a variable */
$data = file_get_contents( 'abc.json' );

/* Decode the json data and create an array */
$json = jsondecode( $data, true );

/* get the array keys */
$keys = array_keys( $json );

/* debug output */
var_dump( $keys );

/* process each node of the json data */
foreach( $json as $arr ){
    foreach( $arr as $obj ) echo $obj->id, $obj->name;
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.