dubbs
dubbs

Reputation: 1199

Echoing nested JSON into PHP

I have a JSON feed which I am parsing via PHP. I am having issues getting some nested elements to echo which I would appreciate some assistance on.. I've looked at loads of related posts here but cant seem to get the logic to work on my specific JSON feed. Could someone advise what I am doing wrong?

JSON feed is here > https://api.lever.co/v0/postings/leverdemo?skip=1&limit=3&mode=json

The elements, I am struggling to parse are the "categories" parent and child nodes of "team", "location" and "commitment".

I was thinking this would work - but it does not...

<?php

$url = 'feed.json'; 
$data = file_get_contents($url); 
$characters = json_decode($data, true);

?>

<table>
<tbody>
    <tr>
        <th>Job title</th>
        <th>Team</th>
        <th>Location</th>
        <th>Commitment</th>
        <th>DescriptionPlain</th>
        <th>applyUrl</th>
    </tr>
    <?php foreach ($characters as $character) : ?>
    <tr>
        <td> <?php echo $character['text']; ?> </td>



        <td> <?php echo $character['categories'][2]['team'] ?></td>
        <td> <?php echo $character['categories'][2]->team ?></td>
        <td> <?php echo $character['categories'][1]->location ?></td>
        <td> <?php echo $character['categories'][0]->commitment ?></td>

        <td> <?php echo $character['descriptionPlain']; ?> </td>
        <td> <?php echo $character['applyUrl']; ?> </td>

    </tr>
    <?php endforeach; ?>
</tbody>

Note, its just the categories children that fail to echo? Also noticed that if I use the full url in the $url variable it all fails? But from local it works??

Any ideas??? Thanks!

Upvotes: 0

Views: 79

Answers (2)

executable
executable

Reputation: 3600

EDIT

You get an error because you are trying to access an object, actually you have an array, here is the solution hope it helps :

<?php
$url = 'https://api.lever.co/v0/postings/leverdemo?skip=1&limit=3&mode=json'; 
$data = file_get_contents($url); 
$characters = json_decode($data, true);
$nb = count($characters);
?>
<table>
<tbody>
    <tr>
        <th>Job title</th>
        <th>Team</th>
        <th>Location</th>
        <th>Commitment</th>
        <th>DescriptionPlain</th>
        <th>applyUrl</th>
    </tr>
    <?php while($nb > 0){ 
    $nb--;
    $nb_lists = count($characters[$nb]['lists']);
    ?>
    <tr>
    <?php 
        while($nb_lists > 0){
            $nb_lists--;
            ?>
        <td> <?php if(isset($characters[$nb]['lists'][$nb_lists]['text'])){ echo $characters[$nb]['lists'][$nb_lists]['text'];} ?> </td>
        <?php } ?>
        <td> <?php if(isset($characters[$nb]['categories']['team'])){echo $characters[$nb]['categories']['team'];} ?></td>
        <td> <?php if(isset($characters[$nb]['categories']['team'])){echo $characters[$nb]['categories']['team'];} ?></td>
        <td> <?php if(isset($characters[$nb]['categories']['location'])) {echo $characters[$nb]['categories']['location'];} ?></td>
        <td> <?php if(isset($characters[$nb]['categories']['commitment'])){ echo $characters[$nb]['categories']['commitment'];} ?></td>
        <td> <?php if(isset($characters[$nb]['descriptionPlain'])){echo $characters[$nb]['descriptionPlain']; }?> </td>
        <td> <?php if(isset($characters[$nb]['applyUrl'])){echo $characters[$nb]['applyUrl'];} ?> </td>
    </tr>
    <?php } ?>
</tbody>

Upvotes: 0

cjs1978
cjs1978

Reputation: 529

It should be:

    <td> <?php echo $character['categories']["team"] ?></td>
    <td> <?php echo $character['categories']["location"] ?></td>
    <td> <?php echo $character['categories']["commitment"] ?></td>

instead. The numeric keys are not present on the array in the data. Also "categories" is not an object, so you cannot use the arrow (->) notation.

Upvotes: 2

Related Questions