isset()似乎不适用于数组

时间:2016-03-01 15:13:35

标签: php isset

我正在研究现有的PHP代码,并尝试理解它以调试问题:

echo "Got here !!!\n";
var_dump($aHeaders);
if (isset($aHeaders['SALESCHANNEL'])) {
    echo "aHeaders['SALESCHANNEL'] is set\n";
    ...
} elseif (isset($aHeaders['SALESUNIT'])) {
    echo "aHeaders['SALESUNIT'] is set\n";
    ...
}
echo "Got there !!!\n";

我得到以下输出:

Got here !!!
array(1) {  
 ["EMAIL,FIRSTNAME,LASTNAME,PHONE,TITLE,DATEOFBIRTH,SALESUNIT,LANGUAGE,STATUS,IS_RESELLER,CUSTOMDATE,ADDRESS_1,ADDRESS_2,CITY,COUNTRY,ZIPCODE,"]=>
int(0) }
Got there !!!

我不是PHP的专家,但我相信我应该看到:

aHeaders['SALESUNIT'] is set

在输出中。出了什么问题?

如果重要的话,我的PHP版本是PHP 5.5.30。

2 个答案:

答案 0 :(得分:7)

你现在拥有的是:

$arr1 = array('EMAIL,FIRSTNAME,LASTNAME,PHONE,TITLE,DATEOFBIRTH,SALESUNIT,LANGUAGE,STATUS');

isset()仅在您拥有以下内容时才有效:

$arr2 = array(
    'EMAIL' => 'blah',
    'FIRSTNAME' => 'blah',
    'LASTNAME' => 'blah',
    'PHONE' => 'blah',
    'TITLE' => 'blah',
    'DATEOFBIRTH' => 'blah',
    'SALESUNIT' => 'blah',
    'LANGUAGE' => 'blah',
    'STATUS' => 'blah'
);

答案 1 :(得分:0)

使用以下代码

echo "Got here !!!\n";
var_dump($aHeaders);
// get the keys to array
$array_keys = array_keys($aHeaders)
// explode the array keys with comma
$array_values = explode(',',$array_keys[0]);
if (in_array('SALESCHANNEL',$array_values)) {
    echo "aHeaders['SALESCHANNEL'] is set\n";
    ...
} elseif (in_array('SALESUNIT',$array_values)) {
    echo "aHeaders['SALESUNIT'] is set\n";
    ...
}
echo "Got there !!!\n";