Php 如何遍历多维数组

Php 如何遍历多维数组,php,arrays,codeigniter,Php,Arrays,Codeigniter,当我发送print\u 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/sup

当我发送print\u 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发送的数组

我如何才能做到这一点?

您的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);
    }
}

提示:循环两次(嵌套)@Uchiha,它不起作用,我试过了,把你的尝试发出去tried@Uchiha,我做了foreach(X作为Y){X1[]=Y;foreach(X1作为X){echo X};但不是workedi我也尝试了,但它显示了一条消息:在foreach的第行为foreach()提供的参数无效。是否有可能在第二次尝试中缺少attachements数组?当“attachments”为空时,您将收到声明的错误。我会在答案中添加这样一个检查。是的,我发现我的AJAX值作为字符串发送时存在一些问题,我缩短了它,现在我的代码使用了2个foreach循环,但是这个stackoverflow页面出现了一些问题,有很多评论,我也发布了一些消息,说明问题已经解决,但在这里看不到@你真的需要第二维度吗?在您提供的代码片段中,它将是无用的;如果您使用第二个维度,请更新您的代码段,或者将此维度标记为答案。