【发布时间】:2022-01-27 07:40:38
【问题描述】:
我试图根据 laravel 中的扩展名对我的数据库进行排序,所以我尝试使用 php pathinfo 并将其与循环混合,但由于某种原因,变量 $ext_name 没有输出文件的真实扩展名。
这是我的代码:
$i = 0;
$pdf_file = [];
$docx_file = [];
$pdf = 0;
$docx = 0;
foreach($files as $i => $the_file){
$path_name[$i] = $the_file["file_path"];
$ext_name[$i] = pathinfo($path_name[$i],PATHINFO_EXTENSION);
if($ext_name[$i]="pdf"){
$pdf_file[$pdf]= $the_file["name"];
}
elseif($ext_name[$i]="docx"){
$docx_file[$docx]= $the_file["name"];
}
++$i;
++$pdf;
++$docx;
}
$path_name 和 $ext_name 的 dd 结果:
$path_name
array:5 [▼
0 => ".........../storage/app/file_processing/blablabla.pdf"
1 => ".........../storage/app/file_processing/blablabla.docx"
2 => ".........../storage/app/file_processing/blablabla.pdf"
3 => ".........../storage/app/file_processing/blablabla.pdf"
4 => ".........../storage/app/file_processing/blablabla.docx"
]
$ext_name
array:5 [▼
0 => "pdf"
1 => "pdf"
2 => "pdf"
3 => "pdf"
4 => "pdf"
]
我不知道 $ext_name 是如何输出相同的扩展名的,我到底哪里做错了?
【问题讨论】:
-
您正在分配值而不是在您的
if语句中进行比较 -
所有的平等都是平等的。一些平等更平等。