Variables in a JSON

Asked

Viewed 122 times

1

Is there any way to vary the value of a JSON? Let’s look at an example:

I have the following JSON

{
    "ISteamClient": 0,
    "ISteamFriends": 0,
    "ISteamUser": 0,
    "IEconItems_440": 0,
    "IEconItems_730": 0,
    "ISteamGameCoordinator_440": 0,
    "ISteamGameCoordinator_570": 0
}

After using

$clientesteam = $json_str["ISteamCliente"];
echo "O Cliente Steam esta $clientesteam";

The serial result: Steam client is 0

Is there any way to turn this "0" into "Online"?

1 answer

6


There are several ways, I don’t know if I understand. For you to get answers focused on what you want you have to work on the question. Bad questions generate off-target answers. But I’ll try to say that if I understand the goal, your problem has nothing to do with JSON or Apis, it’s a very basic algorithm:

$clientesteam = $json_str["ISteamCliente"];
echo "O Cliente Steam esta " . ($clientesteam == 0 ? "Online" : "Offline");

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

Documentation of the ternary operator.

  • In Parts Worked , only resulted in "Online", you can see the result in http://leonardo.noip.me , now to display "Offline" if the value is "1" the code would echo "The Steam Client this " . $clientesteam == 0 ? "Online" : 1 ?" Offline";

  • I updated it in ideone showing that with 1 it works normal. You don’t have to move anything in the syntax to work. In fact I find this manipulation a naive way, I have my doubts whether it will work in all cases but I can only answer upon what you posted.

  • My Code looks like this http://ideone.com/qA4LeS I only want when the value is "0" to be displayed online and when the value is "1" to be displayed offline, because JSON comes from an external website that says whether the Steam Steam ON Client or OFF

  • And what’s the problem you’re seeing in it?

  • the phrase "The Steam Client this" does not appear , it only appears "Online", but I think I decided to do a gambit with html. <p> Steam Client is <?php echo "" . $clientesteam == 0 ? "Online" : "Offline"; ? > </p>

  • Sniffing around, making it work by coincidence is never a good idea. I edited, what I posted here in response is different from what I showed performing, I had forgotten the parentheses. But the demonstration of ideone was already right.

  • It worked perfectly, Thanks.... I ask every stupid question here at Stackoverflow , I’m trying to learn PHP myself.

  • Exactly this is what you need to do. Go learn, don’t try to do without knowing. Fatally you will make many "invisible" mistakes and will not realize until you cause a bigger problem.

  • @Leonardoroberto I don’t think your question is stupid. It may be basic, but everyone who programs in PHP or any.

Show 4 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.