如何遍历多维数组

时间:2015-06-17 10:53:08

标签: php arrays codeigniter

当我发送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         )) 

我想在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发送的数组,

我该怎么做?

2 个答案:

答案 0 :(得分:0)

你的帖子参数“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();

答案 1 :(得分:0)

可以试试这个

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