I have this json a array of object and I want to loop through and display all parents with its child
[
{
"ProfessionName": "Technique ",
"ParentProfessionNum": "",
"ParentProfession": null,
"Id": 1,
"RowVersion": null,
"RowRevision": null
},
{
"ProfessionName": "Production ",
"ParentProfessionNum": "1",
"ParentProfession": null,
"Id": 2,
"RowVersion": null,
"RowRevision": null
},
{
"ProfessionName": "Maintenance ",
"ParentProfessionNum": "1",
"ParentProfession": null,
"Id": 3,
"RowVersion": null,
"RowRevision": null
},
{
"ProfessionName": "Maintenance Unit Manager",
"ParentProfessionNum": "3",
"ParentProfession": null,
"Id": 4,
"RowVersion": null,
"RowRevision": null
},
{
"ProfessionName": "Maintenance operator",
"ParentProfessionNum": "3",
"ParentProfession": null,
"Id": 5,
"RowVersion": null,
"RowRevision": null
},
{
"ProfessionName": "Liquid maintenance operator",
"ParentProfessionNum": "3",
"ParentProfession": null,
"Id": 6,
"RowVersion": null,
"RowRevision": null
},
{
"ProfessionName": "Production Unit Manager",
"ParentProfessionNum": "2",
"ParentProfession": null,
"Id": 7,
"RowVersion": null,
"RowRevision": null
},
{
"ProfessionName": "project manager",
"ParentProfessionNum": "2",
"ParentProfession": null,
"Id": 8,
"RowVersion": null,
"RowRevision": null
},
{
"ProfessionName": "Machine operator",
"ParentProfessionNum": "2",
"ParentProfession": null,
"Id": 9,
"RowVersion": null,
"RowRevision": null
}
]
I did follows first I split parents and child in separate arrays if parent then ParentProfessionNum must be null and if child this should have an ID of another array
$parents = array();
$childs = array();
foreach ($data as $job) {
if (!$job['ParentProfessionNum']) {
array_push($parents, $job);
} else {
array_push($childs, $job);
}
};
then I did a loop using foreach
foreach ($parents as $p) {
echo $p['ProfessionName'];
$parent_id = $p['Id'];
foreach ($childs as $c) {
if ((int) $c['ParentProfessionNum'] == $parent_id) {
echo $c['ProfessionName'];
}
}
}
but this shows only first parent's child.
Update this is the output Technique (parent) Production (child) Maintenance (child)
but also these child are parent for another child I want to display them all as tree view or any other way