为此,我已经命名了第一个图像headImage,并且在移动它时我会跳过它,并完成其他内容。
我已经删除了所有多余的代码,并且只显示导致我的问题的循环:
排序图像数组:
array (size=5)
'headImage' =>
array (size=5)
'name' => string '8-skilling-1606-eller-07-54-1-h93a.jpg' (length=38)
'type' => string 'image/jpeg' (length=10)
'tmp_name' => string '/tmp/phpNUoAtX' (length=14)
'error' => int 0
'size' => int 37748
0 =>
array (size=5)
'name' => string '807003718_2_Big.jpg' (length=19)
'type' => string 'image/jpeg' (length=10)
'tmp_name' => string '/tmp/php1TXBdm' (length=14)
'error' => int 0
'size' => int 36328
如果fileKey是“headImage”,则跳过Foreach循环,并转储fileKey
foreach($uploadFiles as $fileKey => $file){
if($fileKey == "headImage"){
var_dump($fileKey);
continue;
}
}
var_dump的输出:
string 'headImage' (length=9)
int 0
现在,为什么当$ fileKey的值明显不是“headImage”时if语句会运行?
答案 0 :(得分:3)
比较两个字符串的最佳和安全的方法是使用php strcmp函数
像这样使用:
foreach($uploadFiles as $fileKey => $file){
if(strcmp($fileKey ,"headImage") == 0){
var_dump($fileKey);
continue;
}
}
答案 1 :(得分:2)
因为PHP中的"string" == 0
。
您还必须比较使用类型,尝试===
比较:
foreach($uploadFiles as $fileKey => $file){
if($fileKey === "headImage"){
var_dump($fileKey);
continue;
}
}