foreach元素存储在单个数组中

时间:2015-11-02 14:50:21

标签: php arrays foreach

我的foreach元素每次都提供三个变量。

如何将其存储为多维数组。

我存储数组的代码是

foreach($parts as $part){
     $filename = $filename;
     $attachmentid = $attachmentid;
     $filelocation = $filelocation;
     $attachment = [
       'filename' => $filename,
       'attachmnetId' => $attachmentid,
       'filelocation' => $filelocation
     ];
}

我的print_r($attachment)仅显示最后一个foreach数组

2 个答案:

答案 0 :(得分:0)

您需要添加[]以为attachment

创建数组
$attachment[] = [
   'filename' => $filename,
   'attachmnetId' => $attachmentid,
   'filelocation' => $filelocation
 ];

答案 1 :(得分:0)

你有$附件只是一个字符串而不是一个数组

foreach($parts as $part){
 $filename = $filename;
 $attachmentid = $attachmentid;
 $filelocation = $filelocation;
 $attachment[] = [
   'filename' => $filename,
   'attachmnetId' => $attachmentid,
   'filelocation' => $filelocation
 ];
}