PHP循环遍历分解的值以填充数组
在我的数据库中,我存储了一个包含所有上传文档的文件路径。它看起来像下面这样 C:\Users\User\Desktop\Portal\Portal\public\uploads\Weekly Complete Jobs.docx&&C:\Users\User\Desktop\Portal\Portal\public\uploads\Wish List.docx&&& 因此,我使用 &&& 在PHP中,我将分解这个文件路径,然后循环每个路径并将其添加到数组中。现在,我有这样的事情PHP循环遍历分解的值以填充数组,php,arrays,Php,Arrays,在我的数据库中,我存储了一个包含所有上传文档的文件路径。它看起来像下面这样 C:\Users\User\Desktop\Portal\Portal\public\uploads\Weekly Complete Jobs.docx&&C:\Users\User\Desktop\Portal\Portal\public\uploads\Wish List.docx&&& 因此,我使用 &&& 在PHP中,我将分解这个文件路径,然后循环每个路径并将其添加到数组中。现在,我有这样的事情 $attachm
$attachments = array();
if(!empty($this->project->dsReportingDoc->filePath)) {
$string = explode("&&&", $this->project->dsReportingDoc->filePath);
foreach($string as $key=>$path){
if(!empty($path)){
$attDoc = chunk_split(base64_encode(file_get_contents($path)));
$attachments = [(
array(
"Id" => $key,
"Version" => $key,
"File" => array(
"Name" => $path,
"Content" => $attDoc
)
)
)];
}
}
}
我遇到的问题是,它似乎只是将最后一条路径添加到数组中,因为我认为其他路径正在被覆盖
如何将它们全部添加到此阵列中
谢谢看起来您覆盖了$attachments,请尝试$attachments[]
$attachments[] =
array(
"Id" => $key,
"Version" => $key,
"File" => array(
"Name" => $path,
"Content" => $attDoc
)
)
;