Generating details / summary tree from PHP array that contains associative arrays
I am trying to generate a HTML5 details / summary tree using a foreach loop with the following array that contains associative arrays. I am new to working with arrays and I am unsure how to access the correct part of the array to display the industry or the job. Please see the code and comments below.
I am also unsure if I need two foreach loops or if one will do the job. I could potentially make all the elements associative too (as that would make them easier to call) but I am still learning how to do this by trial and error.
I don't yet want to render "Tasks", "Knowledge", "Skills" etc. This will be done on another page.
All help is much appreciated.
$indus['agriculture'] = [
"Picking and Packing" => [
"Tasks" => [
"Entry1", "Entry2", "Entry3", "Entry4", "Entry5",
],
"Tools_used" => [
"Entry1", "Entry2", "Entry3", "Entry4", "Entry5",
],
"Knowledge" => [
"Entry1", "Entry2",
],
"Skills" => [
"Entry1", "Entry2",
],
"Abilities" => [
"Entry1", "Entry2", "Entry3", "Entry4", "Entry5",
],
"data-code" => [
"53-7064.00"
]
],
"Farm Worker" => [
"Tasks" => [
"Entry1", "Entry2", "Entry3",
],
"Tools_used" => [
"Entry1", "Entry2", "Entry3",
],
"Knowledge" => [
"Entry1", "Entry2",
],
"Skills" => [
"Entry1", "Entry2",
],
"Abilities" => [
"Entry1", "Entry2",
],
"data-code" => [
"53-7064.00"
]
],
"Agricultural Equipment Operator" => [
"Tasks" => [
"Entry1", "Entry2",
],
"Tools_used" => [
"Entry1", "Entry2",
],
"Knowledge" => [
"Entry1", "Entry2",
],
"Skills" => [
"Entry1", "Entry2",
],
"Abilities" => [
"Entry1", "Entry2",
],
"data-code" => [
"53-7064.00"
]
],
];
$indus['construction'] = [
"Picking and Packing" => [
"Tasks" => [
"Entry1", "Entry2", "Entry3", "Entry4", "Entry5",
],
"Tools_used" => [
"Entry1", "Entry2", "Entry3", "Entry4", "Entry5",
],
"Knowledge" => [
"Entry1", "Entry2",
],
"Skills" => [
"Entry1", "Entry2",
],
"Abilities" => [
"Entry1", "Entry2", "Entry3", "Entry4", "Entry5",
],
"data-code" => [
"53-7064.00"
]
],
"Farm Worker" => [
"Tasks" => [
"Entry1", "Entry2", "Entry3",
],
"Tools_used" => [
"Entry1", "Entry2", "Entry3",
],
"Knowledge" => [
"Entry1", "Entry2",
],
"Skills" => [
"Entry1", "Entry2",
],
"Abilities" => [
"Entry1", "Entry2",
],
"data-code" => [
"53-7064.00"
]
],
"Agricultural Equipment Operator" => [
"Tasks" => [
"Entry1", "Entry2",
],
"Tools_used" => [
"Entry1", "Entry2",
],
"Knowledge" => [
"Entry1", "Entry2",
],
"Skills" => [
"Entry1", "Entry2",
],
"Abilities" => [
"Entry1", "Entry2",
],
],
"data-code" => [
"53-7064.00"
]
];
$array_keys = array_keys($indus);
foreach($array_keys as $key)
{
echo '<details class="details">';
echo '<summary>';
echo "{$indus[$key]}"; //this should be the industry
echo '</summary>';
echo '<summary>';
echo "{$indus[$key]}"; //this should be the job
echo '</summary> ';
echo '</details>';
}
Answer
Solution:
You need embeded foreach
s to do this task. Note that can also use the keys.
Example: (demo)
// loop around `$indus`:
foreach ($indus as $indusName => $indusData)
{
echo $indusName . "\n";
// loop around `$indus` jobs:
foreach ($indusData as $jobName => $jobData)
{
echo '* ' . $jobName . "\n";
}
}
Will output:
agriculture
* Picking and Packing
* Farm Worker
* Agricultural Equipment Operator
construction
* Picking and Packing
* Farm Worker
* Agricultural Equipment Operator
* data-code
Of course, you could add your HTML tags (<details>
and <summary>
) as needed.