簡體   English   中英

如何遍歷多維數組

[英]How to iterate through a multidimensional array

當我發送print_r命令時,我在數組下面,

Array ( [0] => Array ( [0] => /Applications/AMPPS/www/supportcenter/uploads/attachments/2/cat.jpeg 
[1] => /Applications/AMPPS/www/supportcenter/uploads/attachments/2/images.jpeg 
[2] => /Applications/AMPPS/www/supportcenter/uploads/attachments/2/3672_00116.pdf         )) 

我想回顯或獲取path的值以在codeigniter中作為電子郵件發送,意味着$ attachment = /path/image.png等,因此將附加文件。

$this->email->attach($attachment);

我嘗試了以下方法,但無法正常工作,

$attachments_array[] = $this->input->post('attachments');
    //print_r($attachments_array);

    foreach($attachments_array as $attachment)
    {
        //echo $attachment;
       $this->email->attach($attachment);
    }

    $this->email->send();

$ this-> input-> post('attachments')是用ajax發送的數組,

我怎樣才能做到這一點?

您的post參數“ attachments”似乎是一個數組

$this->input->post('attachments');

因此,您不必將其存儲在另一個數組中(不需要另一個維度)。

正確的代碼如下所示:

//note the missing []
$attachments_array = $this->input->post('attachments');

if(is_array($attachments_array)) { //null would also return false
    foreach($attachments_array as $attachment)
    {
        //echo $attachment;
       $this->email->attach($attachment);
    }
}

$this->email->send();

可以試試這個

 foreach ($attachments_array as $attachment) {
    if (is_array($attachment)) {
        foreach ($attachment as $key => $value) {
            $this->email->attach($value);
        }
    } else {
        $this->email->attach($attachment);
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM