Php foreach循环中的Switch语句失败
我有一个数组:Php foreach循环中的Switch语句失败,php,Php,我有一个数组:$att_arr[]=$attachments 输出为: Array ( [0] => Array ( [1] => Array ( [has_attachment] => 1 [filename] => AWB0091_20170530_26_194710330.pos
$att_arr[]=$attachments
输出为:
Array
(
[0] => Array
(
[1] => Array
(
[has_attachment] => 1
[filename] => AWB0091_20170530_26_194710330.pos
[name] => AWB0091_20170530_26_194710330.pos
)
)
[1] => Array
(
[1] => Array
(
[has_attachment] => 1
[filename] => AWB0091_20170530_35_194511888.pos
[name] => AWB0091_20170530_35_194511888.pos
)
)
[2] => Array
(
[1] => Array
(
[has_attachment] => 1
[filename] => AWB0091_20170530_22_194511888.neg
[name] => AWB0091_20170530_22_194511888.neg
)
)
[3] => Array
(
[1] => Array
(
[has_attachment] => 1
[filename] => AWB0091_20170530_45_194052957.neg
[name] => AWB0091_20170530_45_194052957.neg
)
)
)
现在我尝试使用带有switch语句的foreach循环将细节保存到数据库中,但只插入第一条记录,其余的都失败了。代码如下:
foreach ($attachments_array as $attachment) {
if ($attachment['has_attachment'] == 1) {
$filename = $attachment['name'];
if (empty($filename)) $filename = $attachment['filename'];
$extension = pathinfo($filename)['extension'];
switch ($extension) {
case strtolower("pos"):
$posTextFile = $attachment['name']);
return $this->savePos($posTextFile);
break;
case strtolower("neg"):
$negTextFile = $attachment['name']);
return $this->saveNeg($negTextFile);
break;
default:
return redirect(url(\Config::get('settings.redirect_path')));
}
} else {
return redirect(url(\Config::get('settings.redirect_path')));
}
}
private function savePos ($posTextFile) {
$this->savePosObj->updatePosJob($posTextFile);
return redirect(url(\Config::get('settings.redirect_path')));
}
private function saveNeg ($negTextFile) {
$this->savePosObj->updateNegJob($negTextFile);
return redirect(url(\Config::get('settings.redirect_path')));
}
在这方面有人能帮我吗
$attachment['is_attachment']
没有称为is_attachement的索引,可能您需要尝试
$attachment['has_attachment']
是_attachment
!=<代码>有\u附件。我也不认为你在每个阵列中都做得足够深入var\u dump($attachment)
在您的foreach中查看该属性是否存在。这有什么意义<代码>代码机箱strtolower(“pos”)代码
。我认为您希望使strtolower()超过$extension(如果在切换之前这样做会更好)。在固定字符串上使用strtolower()没有意义(如strtolower('neg')),我觉得您的代码非常复杂。将部分逻辑提取到自己的方法中,减少嵌套,减少else语句。你知道开关是你的问题而不是你的循环吗?也许它只跑了一次,然后跳到了你意想不到的地方。