0

When parsing the json below, the PHP statement:

if (array_key_exists($json_a["Guids"][$g]["Broke"][$b][$a])) {

never evaluates to true despite that "Demo" is a "key" as shown from the print_r statement.

What am I doing wrong on how I'm testing to see if "Demo" or "Live" actually exists in the json? (one or the other or both may be there for any given record)

Thank you.

Json:

{
  "MinimumVersion": "20191101",
  "Guids": {
    "0ebe7e53-12fc-4f8f-a873-4872fe30bbee": {
      "Broke": {
        "Yes": {
          "Demo" : { "Expires" : "" },
          "Live" : { "Expires" : "" }
        },
        "No": {
          "Demo" : { "Expires" : "20191104" },
          "Live" : { "Expries" : "" }
        }
      },
      "Message": "You need to upgrade to the latest version."
    }
  }
}

PHP:

<?php
$string = file_get_contents("json.txt");
$json_a = json_decode($string,true);

$g = "0ebe7e53-12fc-4f8f-a873-4872fe30bbee";
$b = "No";
$a = "Demo";

echo "G: \"" . $g . "\"<br>";
echo "B: \"" . $b . "\"<br>";
echo "A: \"" . $a . "\"<br>";

if (is_array($json_a["Guids"][$g]["Broke"][$b][$a])) {
    #This next line prints Array ([0] => Expires )
    print_r(array_keys($json_a["Guids"][$g]["Broke"][$b][$a]));
} else {
    echo "Test: false";
}

if (array_key_exists($g,$json_a["Guids"])) {
    echo ("true1");
    if (array_key_exists($b,$json_a["Guids"][$g]["Broke"])) {
        echo ("true2");
        if (array_key_exists($json_a["Guids"][$g]["Broke"][$b][$a])) {
            #this never evaluates to true. Why? "Demo" is a "key" as shown from the print_r results statement above.
            echo "Value:\"" . $json_a["Guids"][$g]["Broke"][$b][$a] . "\"<br>";
        }
    }    
}

?>
4
  • array_key_exist need two arguments: the key and the array: see but you do not give the key Demo Commented Nov 4, 2019 at 8:11
  • Please go enable proper PHP error reporting, so that PHP can tell you about such mistakes by itself. Commented Nov 4, 2019 at 8:27
  • @GrenierJ: OMG! Such a STUPID mistake on my part! Thank you for the second set of eyes and spotting that. Commented Nov 4, 2019 at 8:54
  • Why is/was this question voted down? The OP posted a legit question asking what they were doing wrong along with the necessary examples. So the OP forgot to include a parameter in the api call, and probably missed it from staring at the screen for hours on end wondering. Almost all programmers have been there / done that. That doesn't deserve a vote down. Commented Nov 4, 2019 at 13:05

1 Answer 1

2

You're not using array_key_exists properly for that particular case (you use it correctly elsewhere). The correct usage is

array_key_exists ( mixed $key , array $array )

so for what you want to check, you should use

array_key_exists($a, $json_a["Guids"][$g]["Broke"][$b]);
Sign up to request clarification or add additional context in comments.

Comments

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.