1

I'm trying to get some info out of a decoded JSon string into an array.

I've this code:

$json_d='{ // This is just an example, I normally get this from a request...
             "iklive.com":{"status":"regthroughothers","classkey":"domcno"}
}';

$json_a=json_decode($json_d,true);
$full_domain = $domain.$tlds; // $domain = 'iklive' ; $tlds = '.com'

echo $json_a[$full_domain][status];

The problem is, I need to get the value for "status" of "iklive.com" but when I do echo $json_a[$full_domain][status]; it does not work, but if I do it manually like echo $json_a['iklive.com'][status]; (with the quotes there) it works.

I've tried to add the quotes to the variable but without success, how can I do this?

Thanks Everyone!


Thanks to Pekka and jeromegamez I noticed a error in the HTML part of this "problem", the $tlds variable was "com" instead of ".com" -- Sorry by wasting your time with this. I do feel bad now.

Anyway, thanks to jeromegamez and Marc B I discovered that unless status was a constant I need to quote it ;) You can check jeromegamez answer to a detailed explanation of the problem and proper debug.

Sorry.

10
  • And the value of $full_domain is? Commented Nov 3, 2011 at 21:04
  • 1
    Are you sure that $full_domain='iklive.com'? Commented Nov 3, 2011 at 21:04
  • You are 100% positive $full_domain has that value? Then there shouldn't be a problem. Are you keeping in mind that array indexes are case sensitive? Commented Nov 3, 2011 at 21:04
  • 1
    Actually, neither should work, because status hasn't been quoted. Unless you've got a status constant define()'d, a PHP install in strict mode should gripe about both versions. Commented Nov 3, 2011 at 21:04
  • 1
    What do you get exactly when you echo $full_domain ?? And also make sure there's no spaces in there. Commented Nov 3, 2011 at 21:09

1 Answer 1

2

This works for me:

<?php
$json_d='{ "iklive.com":{"status":"regthroughothers","classkey":"domcno"} }';
$json_a = json_decode($json_d, true);

if (!is_array($json_a)) {
    echo "\$json_d is not a valid JSON array\n";
}

$domain = "iklive";
$tld = ".com";
$full_domain = $domain . $tld;

if (!isset($json_a[$full_domain])) {
    echo "{$full_domain} is not set in \$json_a\n";
} else {
    echo $json_a[$full_domain]['status']."\n";
}

What I did:

  • Changed json_a[$full_domain][status] to json_a[$full_domain]['status'] - the missing quotes around status don't break your script, but raise a Notice: Use of undefined constant status - assumed 'status'
  • Added a check if the decoded JSON is actually an array
  • Added a check whether the key $full_domain is set in $json_a
Sign up to request clarification or add additional context in comments.

2 Comments

You're the best! I discovered by problem was a stupid mistake done in the HTML file that posts the $domain and $tld variables. Sorry by wasting your time guys. Anyway, I noticed also I was not using ['status'] quoted! Thanks! ;)
Helped me for Drupal Json Decode as well where it was giving a bunch of similar error messages

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.