我試圖把字符串轉換成一個嵌套的數組
這裏是我的字符串:打開字符串嵌套數組PHP
a/b/d.docx
,我想是這樣的:
array(
"name" => "a",
"type" => "folder",
"sub" => array(
"name" => "b",
"type" => "folder",
"sub" => array(
"name" => "c.docx",
"type" => "file",
"size" => "20"
)
)
)
這是我到目前爲止的代碼
$items = explode('/', $strings);
$num = count($items);
$num = --$num;
$temp = array();
foreach($items as $keys => $value) {
$temp[$keys] = array(
"name" => $value,
"type" => "folder",
"items" => $temp[++$keys]
);
if($keys == $num){
$temp[$keys] = array(
"name" => $value,
"type" => "file",
"size" => "20"
);
}
}
var_dump($temp);
我正在嘗試這個功能,但這只是轉動字符串成一個單一的數組,它也不能做'項'線。
任何幫助將不勝感激。謝謝。
請注意,路徑是虛擬的並且不存在。
更新:我如何添加路徑到每個陣列??例如,"path"=>"a/b"
你」我會重新評論一下,我無法追隨你在做什麼以及你想做什麼。 – paullb
'$ num = - $ num'沒用,你可以使用' - $ num'它會是一樣的 –