foreach循环中的switch语句失败

时间:2017-05-30 15:50:51

标签: php

我有一个数组:$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')));
}

有人可以在这方面帮助我吗?

1 个答案:

答案 0 :(得分:1)

$attachment['is_attachment'] 

没有名为is_attachement的索引,也许你需要尝试 $attachment['has_attachment']