English 中文(简体)
array_key_exists在数组明显具有键时返回false
原标题:array_key_exists returning false when array clearly has key

我正在使用drupal中的节点导入模块进行一些内容导入。我的问题是,我在数据上遇到了错误,看起来应该能顺利工作。这是有争议的代码:

if (count($allowed_values) && !array_key_exists($item[ value ], $allowed_values)) { //$allowed_values[$item[ value ]] == NULL) {
  print "||||" . $item[ value ] . "||||";
  print_r($allowed_values);

这是打印内容的示例:

||||1||||阵列([0]=>;no[1]=>,Zicam®Cold Remedy鼻凝胶喷雾单孔执行器(“Jet”))||||1|||阵列([0]=>;no[1]=>,是)

在我看来,当以“1”清晰可见的方式打印时,它似乎在说“1”不在数组中。如果我用注释掉的检查替换现有的模块代码,则不会引发错误。

最佳回答

你的代码不完整,我无法重现错误。

请允许我调整您的示例:

<?
$item = array( value  => 1);
$allowed_values = array(0 =>  no ,1 =>  yes );

echo "needle:";
var_dump($item[ value ]);
echo "haystack:";
var_dump($allowed_values);

if (count($allowed_values) && !array_key_exists($item[ value ], $allowed_values)) {
        echo "needle hast not been found or haystack is empty
";
} else {
        echo "needle has been found
";
}

给出所需的输出:

needle:int(1)
haystack:array(2) {
  [0]=>
  string(2) "no"
  [1]=>
  string(3) "yes"
}
needle has been found

当您为指针指定字符串而不是整数时,PHP也可以工作。这是一种有损类型的转换,虽然很方便,但也很麻烦。通常你不知道发生了什么,就会导致错误。

但仍然如此。我打赌你的变量类型有问题。

你应该扔掉它们,看看里面到底是什么。

问题回答

暂无回答




相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签