1

My data is being pulled from a client's CMS, but I'm getting strange results

print_r($appliance_data);
foreach ($appliance_data as $adKey => $adValue) {
    print_r($adKey);
    print_r($adValue);
    print_r(array_key_exists($adKey, $appliance_data));
    print_r(isset($appliance_data[$adKey]));
}

With output

Array
(
    [94] => stdClass Object
        (
            [operation] => 0
            [value] => 0
        )

    [102] => stdClass Object
        (
            [operation] => 501
            [value] => 4
        )

    [90] => stdClass Object
        (
            [operation] => 0
            [value] => 0
        )
)
94
stdClass Object
(
    [operation] => 0
    [value] => 0
)
(boolean) FALSE
(boolean) FALSE
102
stdClass Object
(
    [operation] => 501
    [value] => 4
)
(boolean) FALSE
(boolean) FALSE
90
stdClass Object
(
    [operation] => 0
    [value] => 0
)
(boolean) FALSE
(boolean) FALSE

Any idea what's causing this?

Edit: The error is that array_key_exists AND isset return FALSE for a key obtained by looping over an array!

serialize($appliance_data)



a:7:{s:2:"94";O:8:"stdClass":3:{s:9:"operation";s:3:"514";s:5:"value";s:1:"2";s:9:"frequency";s:1:"0";}s:3:"102";O:8:"stdClass":3:{s:9:"operation";s:3:"511";s:5:"value";s:1:"4";s:9:"frequency";s:1:"1";}s:2:"90";O:8:"stdClass":3:{s:9:"operation";s:1:"0";s:5:"value";s:1:"0";s:9:"frequency";s:1:"0";}s:2:"68";O:8:"stdClass":3:{s:9:"operation";s:3:"501";s:5:"value";s:1:"3";s:9:"frequency";s:1:"2";}s:2:"66";O:8:"stdClass":3:{s:9:"operation";s:1:"0";s:5:"value";s:1:"0";s:9:"frequency";s:1:"0";}s:2:"84";O:8:"stdClass":3:{s:9:"operation";s:1:"0";s:5:"value";s:1:"0";s:9:"frequency";s:1:"0";}s:2:"98";O:8:"stdClass":3:{s:9:"operation";s:1:"0";s:5:"value";s:1:"0";s:9:"frequency";s:1:"0";}}
2
  • Some of your code is odd (won't run as it is - too many brackets after print_r()). The loop bit looks like it ought to run fine, whereas the last line should cause an error. Can you confirm this by running it? I get the loop running fine when I try similar code. Commented Apr 4, 2012 at 9:37
  • @Matt, The extra bracket was due to the code being deep within a clients CMS. The array is provided by the client CMS, so I'm thinking its encoded incorrectly! Commented Apr 11, 2012 at 3:32

1 Answer 1

5

You are using a string '94' instead of an int 94. Try:

print_r(array_key_exists(94, $appliance_data));

Edit: I can't reproduce this locally at all - can anyone else?

I tried to replicate this with the following code:

$first = new stdClass();
$first->operation = 0;
$first->value = 0;
$second = new stdClass();
$second->operation = 501;
$second->value = 4;
$third = new stdClass();
$third->operation = 0;
$third->value = 0;

$appliance_data = array(94 => $first, 102 => $second, 90 => $third);
// Same output when using these lines too:
// $appliance_data = array('94' => $first, '102' => $second, '90' => $third);
// $appliance_data = array("94" => $first, "102" => $second, "90" => $third);

print_r($appliance_data);
echo "\n\n";
foreach ($appliance_data as $adKey => $adValue) {
    print_r($adKey);
    echo "\n";
    print_r($adValue);
    print_r(array_key_exists($adKey, $appliance_data));
    echo "\n";
    print_r(isset($appliance_data[$adKey]));
    echo "\n\n";
}

And got this output:

Array
(
    [94] => stdClass Object
        (
            [operation] => 0
            [value] => 0
        )

    [102] => stdClass Object
        (
            [operation] => 501
            [value] => 4
        )

    [90] => stdClass Object
        (
            [operation] => 0
            [value] => 0
        )

)


94
stdClass Object
(
    [operation] => 0
    [value] => 0
)
1
1

102
stdClass Object
(
    [operation] => 501
    [value] => 4
)
1
1

90
stdClass Object
(
    [operation] => 0
    [value] => 0
)
1
1
Sign up to request clarification or add additional context in comments.

10 Comments

It's not a string in the loop though?
Yes, the loop still looks weird.
I suspect this is not directly copy pasted, due to lines like print_r($adValue)); which has too many brackets.
Do you need to reset the array index when calling array_key_exists in a loop? What happens using isset()
Good catch: according to a comment on the man page here php.net/manual/en/function.array-key-exists.php by manhon824, you do need to call reset() first. I tried similar code however, and it worked for the loop structure.
|

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.