'Foreach loop not displaying the 0 element in codeignter

Goodday,

I need some assistant with my foreach loop.

I am uploading multiple images/files to my server and then i am trying to send the images as attachments in an email.

I get the data to the email function and can git it in the loop but for some reason i only see the last item in the array and con see why tjis is happing.

Please see attached the output and code of the function.

function forwardCallEmail($emaildetails)
{
        $data = $emaildetails;
        pre($data['files']);
        echo('<br/>');
        //die;
        $CI = setProtocol();        
        $CI->email->from('');
        $CI->email->subject("");
        $CI->email->message($CI->load->view('calls/forwardCallEmail', $data, TRUE));
        $path = base_url() . "uploads/Calls/";
        foreach ((array) $data['files'] as $files){
            echo($files);
            echo('<br/>');
            $images = explode(',', $files);
            var_dump($images);
            foreach($images as $files);
            echo('<br/>');
            echo $files;
            die;
            $CI->email->attach($path . $files);
            pre($CI);
            die;
        }
        $CI->email->to($data['email']);
        $status = $CI->email->send();

        return $status;

Blockquote



Solution 1:[1]

You can try something like this

Replace this part

foreach ((array) $data['files'] as $files){
        echo($files);
        echo('<br/>');
        $images = explode(',', $files);
        var_dump($images);
        foreach($images as $files);
        echo('<br/>');
        echo $files;
        die;
        $CI->email->attach($path . $files);
        pre($CI);
        die;
    }

To this one

foreach ((array) $data['files'] as $files){
        echo($files);
        echo('<br/>');
        $images = explode(',', $files);
        var_dump($images);
        foreach($images as $files) {
            echo('<br/>');
            echo $files;
            $CI->email->attach($path . $files);
            pre($CI);
        }
    }

Anyway this example to explain a logic of foreach

Ok, just as example

$data = [
    'files' => [
        "image1, image2, image3",
        "image4, image5, image6",
    ]
];

foreach ($data['files'] as $fileKey => $file){

    echo($file);
    $images = explode(',', $file);

    foreach($images as $imageKey => $imageValue) {

        $out[$fileKey][$imageKey] = $imageValue;

    }
}

print_r($out);

And result will be

(
   [0] => Array
       (
           [0] => image1
           [1] =>  image2
           [2] =>  image3
       )

   [1] => Array
       (
           [0] => image4
           [1] =>  image5
           [2] =>  image6
       )

)

Solution 2:[2]

I got it to work, not sure if it is the right way it is working.

Email sending code

CI = setProtocol();        
    
    $CI->email->from('[email protected]', 'HTCC Helpdesk');
    $CI->email->subject("HTCC Call Assistants");
    $CI->email->message($CI->load->view('calls/forwardCallEmail', $data, TRUE));
    $path = 'c:/xampp/htdocs/Helpdeskv2.1/uploads/Calls/';


    $images = explode(',', $data['files']);

        foreach($images as $file){
            $path = 'c:/xampp/htdocs/Helpdeskv2.1/uploads/Calls/'. $file;
            $CI->email->attach($path);
        }

    $CI->email->to($data['email']);
    $status = $CI->email->send();

    return $status;

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Werner Pelser