-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"
                }]
        }]
Suraj Roy
  • 51
  • 2
  • 12
  • you can either use **curl** or **file_get_contents** to achieve this .. – Mittul At TechnoBrave Oct 27 '16 at 06:34
  • please help me with code.. i have googled.. but not found ...i am also new to json.. – Suraj Roy Oct 27 '16 at 06: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 .. – Mittul At TechnoBrave Oct 27 '16 at 06:40
  • @MittulAtTechnoBrave I can see that you're having a hard time getting upvotes, eh? :) – Rax Weber Oct 27 '16 at 06: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. – Mittul At TechnoBrave Oct 27 '16 at 06:43
  • i dont need upvotes.. i am not here to compete with you..i am just trying to have my solution of the problem i am facing.. – Suraj Roy Oct 27 '16 at 06:46
  • @MittulAtTechnoBrave Well, you got it! The point is **contributing** to the community. And answering questions that beginners throw is one way to do it. :) – Rax Weber Oct 27 '16 at 06:46
  • @RaxWeber they wont learn if we provide them a ready code u know .. instead if we just provide them **hint** like documentation link for example, they will **grow** from this small scenarios for sure .. – Mittul At TechnoBrave Oct 27 '16 at 06:47
  • @RaxWeber thank you for your answer.. well its was not about individual at all .. hope it makes you clear. – Mittul At TechnoBrave Oct 27 '16 at 06:50

2 Answers2

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);
?>
Rax Weber
  • 3,730
  • 19
  • 30
  • 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.. – Suraj Roy Oct 27 '16 at 06:44
  • 1
    @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 - http://php.net/manual/en/control-structures.foreach.php will give you better idea .. – Mittul At TechnoBrave Oct 27 '16 at 06:54
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;
}
Professor Abronsius
  • 33,063
  • 5
  • 32
  • 46