Php 转换数组的结构
我有以下数组:Php 转换数组的结构,php,arrays,multidimensional-array,Php,Arrays,Multidimensional Array,我有以下数组: Array ( [0] => Information.pdf [1] => Presentation.pdf [2] => Brochure.pdf ) 我想使用PHP将其转换为以下格式的嵌套数组,以使其与CakePHP 2.1.1电子邮件类兼容: Array ( [Information.pdf] => Array ( [file] => /files/Information.pdf
Array (
[0] => Information.pdf
[1] => Presentation.pdf
[2] => Brochure.pdf
)
我想使用PHP将其转换为以下格式的嵌套数组,以使其与CakePHP 2.1.1电子邮件类兼容:
Array (
[Information.pdf] => Array (
[file] => /files/Information.pdf
[mimetype] => application/pdf
)
[Presentation.pdf] => Array (
[file] => /files/Presentation.pdf
[mimetype] => application/pdf
)
[Brochure.pdf] => Array (
[file] => /files/Brochure.pdf
[mimetype] => application/pdf
)
)
有什么办法吗?“mimetype”可以硬编码
$nested = array();
foreach($flat as $filename) {
$nested[$filename] = array(
'file' => '/files/'.$filename,
'mimetype' => 'application/pdf'
);
};
对于mimetype猜测,将是一个很好的选择
$newArr = array();
foreach($arr as $val) {
$newArr[$val] = array(
"file" => "/files/".$val,
"mimetype" => "application/pdf"
);
}
对于mimetype猜测,将是一个不错的选择。使用
foreach
迭代数组项并使用获取mimetype
$newArr = array();
foreach($arr as $val) {
$newArr[$val] = array(
"file" => "/files/".$val,
"mimetype" => "application/pdf"
);
}
$temp = array(); //A temporary array to store new array
foreach($array as $filename) { //Supposing $array is your original array
$file = '/files/'.$filename;
$temp[$filename] = array(
'file' => $file,
'mimetype' => mime_content_type($file)
);
};
使用
foreach
遍历数组项,并使用获取mimetype
$temp = array(); //A temporary array to store new array
foreach($array as $filename) { //Supposing $array is your original array
$file = '/files/'.$filename;
$temp[$filename] = array(
'file' => $file,
'mimetype' => mime_content_type($file)
);
};
我只是做出了回应。我只是做出了回应。