简体   繁体   中英

Switch statement inside a foreach loop is failing

I have an array: $att_arr[] = $attachments

Output is:

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
                )
        )
)

Now I am trying to use a foreach loop with switch statements to save the details to database but only the first record is inserted and the rest all fails. Here is the code:

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')));
}

Can anyone please help me out in this regard?

$attachment['is_attachment'] 

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM