0

i have a json data like so

{
    "code": 1,
    "data": [
        {
            "apple": [
                {
                    "id": 127,
                    "type": 1,
                    "color": green,                   
                    "stage": 1,                    
                    "status": 1                   
                },
                {
                    "id": 128,
                    "type": 2,
                    "color": red,                   
                    "stage": 1,                    
                    "status": 1  
                }
            ]
        },
        {
            "oranges": [
                {
                    "id":133
                   "type": 3,
                    "color": rainbow,                   
                    "stage": 1,                    
                    "status": 1   
                },
                {
                   "id":134
                   "type": 3,
                    "color": black,                   
                    "stage": 1,                    
                    "status": 1
                }
            ]
        },
        {
            "berry": [
                {
                    "id":4
                   "type": 2,
                    "color": white,                   
                    "stage": 1,                    
                    "status": 1
                }
            ]
        },
        {
            "watermelon": [
                {
                    "id":5
                   "type": 2,
                    "color": red and blue,                   
                    "stage": 1,                    
                    "status": 1
                }
            ]
        }
    ],
    "bleh": "Succesfully queried database"
}

i would like to create a table in php that goes somthing like this

Fruit |  Type | color
apple    1     green
apple    2     red
oranges  3     rainbow
oranges  3     black

so basically what i want is when ever a object like apple has more then one array inside it the table to display apple and the corresponding data to it. this is what i have so far

 $output = json_decode(JsonData);
     $result =$output['data'][0]['apple'];

<table>
    <thead> 
            <tr>                
                <th>Fruits</th> 
                <th>Type</th> 
                <th>color</th>                 
            </tr> 
            </thead> 
            <tbody> 
        <?php if(!isset($result)){ ?>
        <tr> 
            <td>Empty</td> 
            <td>Empty</td> 
            <td>Empty</td> 
            <td>Empty</td>       
        </tr> 
        <?php else{
            foreach($result as $apples){ ?>              
            <tr> 
                <td></td> 
                <td><?php echo "$apples["type"]";?></td> 
                <td><?php echo "$apples["color"]";?></td>                    
            </tr> 
          <?php } ?>
        <?php } ?>
            </tbody> 
        </table>
1
  • whats this <?php echo "apples["type"]";?> Commented Apr 18, 2018 at 0:52

3 Answers 3

1
$arr = json_decode($json, true); 
foreach($arr['data'] as $fruit => $types){
 foreach($types as $info){
 echo $fruit;
 echo $info['type']; 
 echo $info['color'];
}
}

This is how you can get your JSON data into PHP. I trust you can create an HTML table or something to display this information.

Sign up to request clarification or add additional context in comments.

2 Comments

hello when i echo $fruit i get a output as 0123
also $info['types']; says undefended index
1

Well just at a glance this is completely wrong syntax

<?php echo "apples["type"]";?>

Probably you need something like this

<?php echo $apples["type"];?>

What you had will probably give you a syntax error because PHP will see it like this

<?php echo "apples["   type   "]";    ?>

Where "apples[" is a complete string, type is an undefined constant that is unexpected in the location it's in, and then an extra string "]";

you can test it in this sandbox

https://3v4l.org/nR8vF

Which gives us this

Parse error: syntax error, unexpected 'type' (T_STRING), expecting ',' or ';' in /in/nR8vF on line 5

UPDATE

Well there are a few other obvious problems, just with this

$output = json_decode(JsonData);
$result = $output['data'][0]['apple'];

I assume JsonData is just a placeholder or are you missing the $ on that too?

Then as you don't have the second argument set to true, then data will be object style. Personally I would do

 $output = json_decode(JsonData, true);

And just use it as an array.

Others have covered the double "foreach" deal (for looping on fruit), so I won't re-hash that, I would have covered it but it was "Dinner" time and my wife gets a bit "Irritated" when I mess with code instead of coming up from the office (I have an office in the basement).

2 Comments

its the same thing it doesn't matter if its inside a " its just easier to concatenate stuffs like that
It's not the same because "apples is a string and $apples is a variable. You may have an argument if you had, had "$apples['type']" but that's not what you have. Even then I prefer to wrap it in {} such as "{$apples['type']}" as it makes it much more cleaner.
0

If you are looking to show all fruits then you will need something along the lines of:

$result =$output['data'];

Then update your foreach loop

foreach($result as $key=>$fruit){ 
    foreach($fruit as $k=>$dets){?>              
        <tr> 
            <td></td> 
            <td><?php echo $dets["type"];?></td> 
            <td><?php echo $dets["color"];?></td>                    
        </tr> 
<?php } 
} ?>

That's also not valid json

1 Comment

why do u say its not valid?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.