$className = "Class B: Wednesday 6pm";
// $studentArray is multidimensional array with student info
foreach($studentArray as $student) {
echo array_search($className,$student);
}
// Contents of $student is
Array
(
[Groups] => 187,267
[Birthday] => DateTime Object
(
[date] => 1981-02-04 00:00:00.000000
[timezone_type] => 3
[timezone] => UTC
)
[_IGTestScore] => 0
[Email] => blank@blank.com
[_IGClass1] => Class B: Wednesday 6pm
[_IGAttendedClass1] => DateTime Object
(
[date] => 2016-02-17 00:00:00.000000
[timezone_type] => 3
[timezone] => UTC
)
[FirstName] => Joe
[Id] => 3
[LastName] => Schmoe
)
输出是:
_IGTestScore
如果我在$ className和$ student [' _IGClass1]上执行var_dump,我会得到:
字符串(22)" B类:星期三下午6点"
字符串(22)" B类:星期三下午6点"
以前从来没有遇到过array_search的问题,但这让我发疯了,似乎无法弄清楚这里发生了什么。似乎任何字符串搜索都是相同的。但是,如果我搜索一个整数,比如3,它会拉出" Id"正确。
Array
(
[0] => Array
(
[Groups] => 187,267
[Birthday] => DateTime Object
(
[date] => 1981-02-04 00:00:00.000000
[timezone_type] => 3
[timezone] => UTC
)
[_IGTestScore] => 0
[Email] => blank@blank.com
[_IGClass1] => Class B: Wednesday 6pm
[_IGAttendedClass1] => DateTime Object
(
[date] => 2016-02-17 00:00:00.000000
[timezone_type] => 3
[timezone] => UTC
)
[FirstName] => Joe
[Id] => 3
[LastName] => Schmoe
)
)
答案 0 :(得分:2)
我发现了问题......但我不知道原因:对我来说,这是一个谜!
问题是_IGTestScore
为整数:如果_IGTestScore
是整数,array_search()
返回_IGTestScore
(实际上它会为任何搜索字符串返回_IGTestScore
,甚至如果它们不存在!),如果_IGTestScore
设置为'0'
(字符串)或正整数,array_search()
将返回正确的值!
的 Try & See 强>
这是一个错误?
有人有解释吗?
修改强>
显然,有一个解决方案:使用“strict”参数:
array_search( $className, $student, True );
但是......为什么“B级:星期三下午6点”== 0?