0

I am pulling JSON data from a URL and attempting to display a list of just the display names.

In a way, this would be very easy too loop through if I knew that X amount of results would return each time. However, the results returned will vary from 0 to 50+.

I have done plenty of searches that all say "just use json_decode"... not so much the case.

I have the following JSON:

{
    "ACK": "SUCCESS",
    "ERROR": null,
    "AGENT": {
        "has_results": 1,
        "agents": [
            {
                "display_name": "Alex",
                "time_in_state": "5214",
                "state": "Aux",
                "callstakentoday": null,
                "callsouttoday": null,
                "agntpriority": null,
                "skill_num": "76"
            },
            {
                "display_name": "Bill",
                "time_in_state": "6312",
                "state": "Aux",
                "callstakentoday": null,
                "callsouttoday": null,
                "agntpriority": null,
                "skill_num": "76"
            },
            {
                "display_name": "Carrie",
                "time_in_state": "5982",
                "state": "Aux",
                "callstakentoday": null,
                "callsouttoday": null,
                "agntpriority": null,
                "skill_num": "76"
            },
            {
                "display_name": "David",
                "time_in_state": "4226",
                "state": "Aux",
                "callstakentoday": null,
                "callsouttoday": null,
                "agntpriority": null,
                "skill_num": "76"
            }
        ]
    },
    "SKILL": {
        "has_results": 1,
        "num_skills": 1,
        "skills": [
            {
                "display_name": "Phone Skills",
                "skill_num": "76",
                "callsinqueue": "0",
                "callstoday": "9",
                "abandtoday": "0",
                "lwt": "0",
                "ewt": "0",
                "servicelvl": "100",
                "avgspeedans": "6",
                "talktime": "289"
            }
        ]
    },
    "TIME": 1383766541
}

From the examples and documentation I have read, the following code has been created:

<?php
    $url="http://myjsonurl.local";

    $json = file_get_contents($url);
    //echo $json; 

    $json = json_decode($json);
    foreach($json as $item->display_name)
    {
            echo $item->agents->display_name;
    }
?>

My end goal is to have a list of only names which I can then display in an alternate webpage.

So my question is... how do I read this page and format the data nicely (perhaps an array I can just print?) so I can utilize it in the future?

4 Answers 4

3

You have the following in your code:

foreach($json as $item->display_name)    

This is incorrect and doesn't do what you want. As you can see by doing a print_r($json), the names are in $json->AGENT->agents, so you'll want to loop through those items and then traverse the display_name using arrow syntax ($item->display_name). Once you have the display name, you can push it into an array, and use it however you want.

Your loop should look like below:

$names = array(); // initialize empty array
foreach($json->AGENT->agents as $item)
{
    $names[] = $item->display_name;
}

print_r($names); // see the array contents

Output:

Array
(
    [0] => Alex
    [1] => Bill
    [2] => Carrie
    [3] => David
)

Demo.

Note: If you don't know the structure of the JSON object beforehand, then you can use a nested foreach loop to retrieve the names.

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

4 Comments

Thank you Amal. This is definitely going in the right direction now. Another quick question if I may... I thought it would be cool to color code names based on their state in the JSON. I could change the JSON decode to store the state, but how would I best tie the names and states together to display the data?
@Fairplay89: That's a different problem (and beyond the scope of this answer, I think). Can you please post that as a new question and explain what you're trying to do in more detail? :)
Sorry... just brainstorming out loud. Let me do some digging on my own before clobbering up the boards. Thanks!
@Fairplay89: That's a good idea. Read this meta post :)
1

The array you want to iterate is $json->AGENT->agents. Also, your foreach syntax is wrong.

Try:

foreach($json->AGENT->agents as $item)
{
        echo $item->display_name;
}

Comments

0

If you want to get the JSON data as an array rather then an object, use json_decode($json, true) instead. This tells the function to return it as an associative array, as you can read here: http://php.net/manual/en/function.json-decode.php

Comments

0

You have an error in your foreach loop.

foreach($json as $item->display_name)
    {
            echo $item->agents->display_name;
    }

$item is not an object and you are actually trying in this line to access property "display_name" of none object ($item)

foreach($json as $item)

2 Comments

@iambriansreed Pointing to an error like this should often be enough for OP to see what went wrong and how to fix it. Certainly not worth a downvote.
@GolezTrol I agree, not worth a down vote, which is why I didn't.

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.