当我遍历关联数组时,字符串索引将转换为整数。
为什么会这样?
$day_of_the_week = array(
"1" => 'Monday',
"2" => 'Tuesday',
"3" => 'Wednesday',
"4" => 'Thursday',
"5" => 'Friday',
"6" => 'Saturday',
"7" => 'Sunday'
);
foreach($day_of_the_week as $index => $value){
echo "Index: $index<br />Type: " . gettype($index) . "<br />Value:$value<br /><br />";
}
输出:
Index: 1
Type: integer
Value: Monday
Index: 2
Type: integer
Value: Tuesday
Index: 3
Type: integer
Value: Wednesday
Index: 4
Type: integer
Value: Thursday
Index: 5
Type: integer
Value: Friday
Index: 6
Type: integer
Value: Saturday
Index: 7
Type: integer
Value: Sunday
使用strpos($ haystack,$ needle)函数时会出现问题,因为$ needle参数:&#34;转换为整数并应用为字符的序数值。&#34;
一个简单的解决方案是将strval函数抛出到strpos函数中:
strpos($haystack,strval($needle))