I have documents stored on my server, and their information like title, filepath, date, category, etc., stored in a database. My goal is to be able to group the documents together by their categories. The document's category is stored in the database as a string like "COVID-19 | Guidance | Mortuary Affairs"
, and I want to convert it to an array like the following:
[
"COVID-19" => [
"Guidance"=> [
"Mortuary Affairs" => [
// Where the document info will be
]
]
]
]
The first step would be looping through the documents and exploding each category by the |
delimiter:
foreach($all_documents as $document)
{
$categories = array_map('trim', explode('|', $document['category']));
}
Doing so results in the following:
[
"COVID-19",
"Guidance",
"Mortuary Affairs"
]
How would I then take this array and turn it into the nested array displayed at the top?