I have the following heirachical array:
Array
(
[0] => stdClass Object
(
[name] => MD
[children] => Array
(
[0] => stdClass Object
(
[name] => Year 1
[children] => Array
(
[0] => stdClass Object
(
[name] => Integrated Medical Sciences 1...
I want to print the names in the array as per the heirachical relationships as an unordered list. I have tried:
function walk($array){
foreach ($array as $key => $value) {
echo "<ul>";
if(!is_array($value->name)){
echo "<li>$key:[$value->name]</li>";
walk($value);
}
echo "</ul>";
}
}
walk($roots);
But get the following, which contains the correct names and relationships, but also errors:
0:[MD]
Notice: Trying to get property of non-object in /var/www/html/md/json/generate_json_by_year_print.php on line 65 Notice: Trying to get property of non-object in /var/www/html/md/json/generate_json_by_year_print.php on line 66
name:[]
Warning: Invalid argument supplied for foreach() in /var/www/html/md/json/generate_json_by_year_print.php on line 63
Notice: Trying to get property of non-object in /var/www/html/md/json/generate_json_by_year_print.php on line 65 Notice: Trying to get property of non-object in /var/www/html/md/json/generate_json_by_year_print.php on line 66
children:[]
0:[Year 1]
Notice: Trying to get property of non-object in /var/www/html/md/json/generate_json_by_year_print.php on line 65 Notice: Trying to get property of non-object in /var/www/html/md/json/generate_json_by_year_print.php on line 66
name:[]
Warning: Invalid argument supplied for foreach() in /var/www/html/md/json/generate_json_by_year_print.php on line 63
Notice: Trying to get property of non-object in /var/www/html/md/json/generate_json_by_year_print.php on line 65 Notice: Trying to get property of non-object in /var/www/html/md/json/generate_json_by_year_print.php on line 66
children:[]
0:[Integrated Medical Sciences 1]
Notice: Trying to get property of non-object in /var/www/html/md/json/generate_json_by_year_print.php on line 65 Notice: Trying to get property of non-object in /var/www/html/md/json/generate_json_by_year_print.php on line 66
name:[]
Line 63 is foreach ($array as $key => $value) {
Line 65 is if(!is_array($value->name)){
Line 66 is echo "<li>$key:[$value->name]</li>";
UPDATE
OK tried:
function walk($array){
foreach ($array as $key => $value) {
echo "<ul>";
if(!is_array($value->name)){
echo "<li>$key:[$value->name]</li>";
if (isset($value->children)) {
walk($value->children);
}
}
echo "</ul>";
}
}
walk($roots);
Which produces:
0:[MD]
0:[Year 1]
0:[Integrated Medical Sciences 1]
1:[Integrated Medical Sciences 2]
1:[Year 2]
0:[Integrated Medical Practice 1]
0:[Centralised Teaching]
0:[Seminar - General Medicine Student Led Presentations]
1:[Surgery - CBL]
1:[Clinical Preparation]
0:[Clinical Skills - Respiratory History Taking]
1:[Seminar - Airways Disease]
2:[Seminar - Approach to Head Injury and Altered Mental State]
3:[Seminar - Atherothrombosis]
4:[Seminar - Breast Cancer]
5:[Seminar - Collapse]
6:[Seminar - Cough]
7:[Seminar - CVS Examination]
8:[Seminar - Fatigue]
9:[Seminar - GIT Examination]
10:[Seminar - MSK Examination]
11:[Seminar - Overview of CVS Disease]
12:[Seminar - Overview of Gastroenterology ]
13:[Seminar - Overview of Kidney and Urological Disease]
14:[Seminar - Overview of Neurological Disease ]
15:[Seminar - PVD]
16:[Seminar - Spinal pain]
17:[Seminar - Stroke]
18:[TBL - Cardiac Conduction]
19:[TBL - CRC]
20:[TBL - Day 2 post ACS]
21:[TBL - Infectious Gastro ]
22:[TBL - Liver Disease]
23:[TBL - Polyarthritis]
24:[TBL - Sepsis]
25:[TBL - Trauma and Fracture]
26:[TBL - VTE]
27:[Tutorial - Clinical Skills - Gastrointestinal History Taking]
28:[Tutorial - CVS Chest Pain and Associated Symptoms]
2:[Geriatric Medicine]
3:[Internal Medicine]
0:[RPH - Bedside Tutorial - Gastro]
1:[RPH - Bedside Tutorials CVS and Resp]
Which is great. But I only want the names as text, not numbers and square brackets...
UPDATE
This is what I ended up using:
function walk($array)
{
//convert object to key-value array
if (is_object($array)) {
$array = (array)$array;
}
echo "<ul>";
foreach ($array as $key => $value) {
if (is_int($value) || is_string($value)) {
echo "<li>" . $value;
} elseif (is_array($value) || is_object($value)) {
walk($value);
}
echo "</li>";
}
echo "</ul>";
}
walk($roots);
walk($value->children);childrenproperty might not exist (instead to an empty array - you don't provide enough sample data to know) you will needif (isset($value->children)) walk($value->children);