Jump to content

Recommended Posts

Hello, 

 

I will try to keep this short and sweet. This small script I have the data source has changed and I need a way to trigger an online and offline tag. Which was really easy with how the JSON data file was set up before. Now things are a little more complicated and I am not sure how to approach this. 

 

Here is the code I am working with that needs to be updated. I need to change out where the $status varible is to use the [name] key and if there is a value then show as online. If it is empty or doesn't exist show as offline. As this key isn't a normal true false value how can I approach this so that if the key has a value then set as true?

// Lets get and parse the data and create the variables
  $json_string = file_get_contents($jsonPath);
  $parsed_json = json_decode($json_string);
  $status = ($parsed_json->active) ? 'true' : 'false';

// Lets assembly the banners to display
$online = '<span class="label label-success"> ONLINE</span>';

$offline = '<span class="label label-danger"> OFFLINE</span>';

if ($status == "true") {
    echo $online;
} else {
    echo $offline;
}

Here is a sample of the JSON file which was XML that I have parsed to JSON to get the keys. I need to get to the [name] and if there is a vaule set as true. Hopefully this makes sense. I can't find anything that talks about this but, then again. I am really not sure how to word it or what exactly I am looking for. 

Array
(
    [0] => Array
        (
            [name] => live
            [live] => Array
                (
                    [stream] => Array
                        (
                            [name] => rcontreras
                            [time] => 2969160
                            [bw_in] => 342512
                            [bytes_in] => 186226066
                            [bw_out] => 533112
                            [bytes_out] => 285996865
                            [bw_audio] => 0
                            [bw_video] => 342512
                            [client] => Array

If anyone can offer some assistance that would be great! This has been bothering me for weeks. 

-Thanks

Link to comment
https://forums.phpfreaks.com/topic/301195-parsing-json-with-php/
Share on other sites

As the output shows the first layer of the "JSON" is an array with a numeric key. That suggests it could contain more than one element. Is that possible? If so, what will you do with the multiple items in the array?

 

Ultimately you will have the second array. From there it's either ->name or ->live->stream->name. Assuming you are still decoding as an object, as the output shows it using arrays instead.

As the output shows the first layer of the "JSON" is an array with a numeric key. That suggests it could contain more than one element. Is that possible? If so, what will you do with the multiple items in the array?

 

Ultimately you will have the second array. From there it's either ->name or ->live->stream->name. Assuming you are still decoding as an object, as the output shows it using arrays instead.

 

 

Yes it is possible that it could contain more than one element. 

 

What will I do multiple items in the array? Hopefully ignore them. For what I am trying to do right now I just need the name and hopefully I can use that as a value to trigger a true or false so I can show online or offline. This XML file doesn't leave much to work with. I wish it had an active key under a certain element for a user like the old one had but, it does not. 

 

I don't need the value of [name] persay, but rather use that as if that value exists then true if not false. Is something like the possible?

 

-Thanks

You need to drill into the outer array in order to get to the inner ones. So, if you're actually going to ignore any data other than the contents of the first array index, you'd use

$parsed_json[0]->name;

to get the value of 'name' in that first array value.

 

I don't know the business logic behind the feed you're parsing, so I'm not sure if just checking the existence of the name value will work, but the implication is that if the name field contains data, that user is online, right? If so, wouldn't it be possible to assume that if there's a record, there's an online user? Seems like it'd be a bit wasteful to populate a feed stream with empty data. At any rate, you could do something like

$status = !empty($parsed_json[0]->name) ? true : false;
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.