PHP数组_key_存在,返回错误

PHP数组_key_存在,返回错误,php,array-key-exists,Php,Array Key Exists,我的数据是从客户的CMS中提取的,但我得到了奇怪的结果 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]))

我的数据是从客户的CMS中提取的,但我得到了奇怪的结果

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]));
}
有输出

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
知道是什么引起的吗

编辑:错误是数组\u key\u存在,对于通过循环数组获得的密钥,isset返回FALSE

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";}}

您正在使用字符串“94”而不是int 94。尝试:

print_r(array_key_exists(94, $appliance_data));
编辑:我根本无法在本地复制此内容-其他人可以吗

我尝试用以下代码复制此代码:

$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";
}
得到了这个输出:

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

您正在使用字符串“94”而不是int 94。尝试:

print_r(array_key_exists(94, $appliance_data));
编辑:我根本无法在本地复制此内容-其他人可以吗

我尝试用以下代码复制此代码:

$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";
}
得到了这个输出:

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

循环中不是字符串?是的,循环看起来仍然很奇怪。我怀疑这不是直接复制粘贴的,因为像print_r($adValue));括号太多。在循环中调用
array\u key\u exists
时是否需要重置数组索引?使用
isset()
Good catch会发生什么:根据manhon824在手册页上的评论,您确实需要首先调用reset()。然而,我尝试了类似的代码,它对循环结构起了作用。虽然它不是循环中的字符串?是的,循环看起来仍然很奇怪。我怀疑这不是直接复制粘贴的,因为像print_r($adValue));括号太多。在循环中调用
array\u key\u exists
时是否需要重置数组索引?使用
isset()
Good catch会发生什么:根据manhon824在手册页上的评论,您确实需要首先调用reset()。然而,我尝试了类似的代码,它适用于循环结构。循环位看起来应该运行正常,而最后一行应该会导致错误。你能通过运行它来确认这一点吗?当我尝试类似的代码时,循环运行得很好。@Matt,额外的括号是因为代码深入到一个CMS中。该阵列是由客户端CMS提供的,所以我认为它的编码不正确!您的一些代码很奇怪(无法按原样运行-print_r()后的括号太多)。循环位看起来应该运行正常,而最后一行应该会导致错误。你能通过运行它来确认这一点吗?当我尝试类似的代码时,循环运行得很好。@Matt,额外的括号是因为代码深入到一个CMS中。该阵列是由客户端CMS提供的,所以我认为它的编码不正确!