Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php foreach元素存储在单个数组中_Php_Arrays_Foreach - Fatal编程技术网

Php foreach元素存储在单个数组中

Php foreach元素存储在单个数组中,php,arrays,foreach,Php,Arrays,Foreach,我的foreach元素每次提供三个变量 如何将其存储为多维数组 我存储数组的代码是 foreach($parts as $part){ $filename = $filename; $attachmentid = $attachmentid; $filelocation = $filelocation; $attachment = [ 'filename' => $filename, 'attachmnetId' =>

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

如何将其存储为多维数组

我存储数组的代码是

foreach($parts as $part){
     $filename = $filename;
     $attachmentid = $attachmentid;
     $filelocation = $filelocation;
     $attachment = [
       'filename' => $filename,
       'attachmnetId' => $attachmentid,
       'filelocation' => $filelocation
     ];
}
我的print\u r$附件只显示最后一个foreach数组

您需要添加[]来创建附件数组


您将$attachment作为字符串而不是数组

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

foreach后面的前3行看起来很没用。
foreach($parts as $part){
 $filename = $filename;
 $attachmentid = $attachmentid;
 $filelocation = $filelocation;
 $attachment[] = [
   'filename' => $filename,
   'attachmnetId' => $attachmentid,
   'filelocation' => $filelocation
 ];
}