如何反转嵌套数组 PHP 的键=>值顺序?
How to reverse key=>value order of a nested array PHP?
所以数组看起来像这样:
$folderIDs = array(
"FolderID"=>0,
"FolderName"=>"Main Folder",
"Folders"=>array(
array(
"FolderID"=>23,
"FolderName"=>"First SubFolder",
"Folders"=>array()
),
array(
"FolderID"=>4,
"FolderName"=>"Second SubFolder",
"Folders"=>array(
array(
"FolderID"=>99,
"FolderName"=>"SecondSub SubFolder ",
"Folders"=>array(
array(
"FolderID"=>27,
"FolderName"=>"SecondSubSub SubFolder",
"Folders"=>array()
)
)
)
)
),
array(
"FolderID"=>46,
"FolderName"=>"Third SubFolder",
"Folders"=>array()
)
)
);
数组可以 "more" 嵌套,因为 subsubsub 文件夹也可以有子文件夹等等。
我们想要一个看起来像这样的重构数组:
$reconstructed_folderIDs = array(
0 => array(
"FolderName" => "Main Folder",
"ParentFolder" => "",
),
23 => array(
"FolderName" => "First SubFolder",
"ParentFolder" => 0,
),
4 => array(
"FolderName" => "Second SubFolder",
"ParentFolder" => 0,
),
99 => array(
"FolderName" => "SecondSub SubFolder",
"ParentFolder" => 4,
),
127 => array(
"FolderName" => "SecondSubSub SubFolder",
"ParentFolder" => 99,
),
46 => array(
"FolderName" => "Third SubFolder",
"ParentFolder" => 0,
),
);
是否有快速或可行的解决方案?
提前致谢!
下面是一种相当糟糕的方法来获得你需要的结果,快速但肮脏。
function process(&$result, $record, $parent = NULL) {
$f_id = $record['FolderID'];
$f_name = $record['FolderName'];
$f_data = $record['Folders'];
if (!isset($result[$f_id]))
$result[$f_id] = [];
$result[$f_id]['FolderName'] = $f_name;
$result[$f_id]['ParentFolder'] = $parent;
if (empty($f_data)===FALSE)
foreach( $f_data as $useless => $folder_record )
process($result, $folder_record, $f_id);
}
$reconstructed_folderIDs = [];
process($reconstructed_folderIDs, $folderIDs );
所以数组看起来像这样:
$folderIDs = array(
"FolderID"=>0,
"FolderName"=>"Main Folder",
"Folders"=>array(
array(
"FolderID"=>23,
"FolderName"=>"First SubFolder",
"Folders"=>array()
),
array(
"FolderID"=>4,
"FolderName"=>"Second SubFolder",
"Folders"=>array(
array(
"FolderID"=>99,
"FolderName"=>"SecondSub SubFolder ",
"Folders"=>array(
array(
"FolderID"=>27,
"FolderName"=>"SecondSubSub SubFolder",
"Folders"=>array()
)
)
)
)
),
array(
"FolderID"=>46,
"FolderName"=>"Third SubFolder",
"Folders"=>array()
)
)
);
数组可以 "more" 嵌套,因为 subsubsub 文件夹也可以有子文件夹等等。
我们想要一个看起来像这样的重构数组:
$reconstructed_folderIDs = array(
0 => array(
"FolderName" => "Main Folder",
"ParentFolder" => "",
),
23 => array(
"FolderName" => "First SubFolder",
"ParentFolder" => 0,
),
4 => array(
"FolderName" => "Second SubFolder",
"ParentFolder" => 0,
),
99 => array(
"FolderName" => "SecondSub SubFolder",
"ParentFolder" => 4,
),
127 => array(
"FolderName" => "SecondSubSub SubFolder",
"ParentFolder" => 99,
),
46 => array(
"FolderName" => "Third SubFolder",
"ParentFolder" => 0,
),
);
是否有快速或可行的解决方案?
提前致谢!
下面是一种相当糟糕的方法来获得你需要的结果,快速但肮脏。
function process(&$result, $record, $parent = NULL) {
$f_id = $record['FolderID'];
$f_name = $record['FolderName'];
$f_data = $record['Folders'];
if (!isset($result[$f_id]))
$result[$f_id] = [];
$result[$f_id]['FolderName'] = $f_name;
$result[$f_id]['ParentFolder'] = $parent;
if (empty($f_data)===FALSE)
foreach( $f_data as $useless => $folder_record )
process($result, $folder_record, $f_id);
}
$reconstructed_folderIDs = [];
process($reconstructed_folderIDs, $folderIDs );