[英]loop through json array of objects in php
I have this json a array of object and I want to loop through and display all parents with its child我有这个 json 一个对象数组,我想循环并显示所有父母及其孩子
[
{
"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我首先遵循我将父级和子级拆分为单独的数组,如果父级然后 ParentProfessionNum 必须为空,如果 child 这应该有另一个数组的 ID
$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 做了一个循环
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但这些孩子也是另一个孩子的父母我想将它们全部显示为树视图或任何其他方式
You have never set $parent_id
... use instead $p['id']
or set $parent_id = $p['id']
您从未设置$parent_id
... 使用$p['id']
或设置$parent_id = $p['id']
Also the (int)
cast is useless until you use ==
because PHP will check for equality of value and not type, so 1=="1"
is true, instead 1==="1"
is false, and so the cast is needed此外, (int)
在您使用==
之前是无用的,因为 PHP 将检查值的相等性而不是类型,因此1=="1"
为真,而不是1==="1"
为假,因此转换需要
So the code should be所以代码应该是
foreach ($data as $p) {
echo "PARENT : ".$p->ProfessionName."\n";
$parent_id = $p->Id;
echo "CHILDREN : \n";
foreach ($data as $c) {
if ($c->ParentProfessionNum == $parent_id) {
echo $c->ProfessionName."\n";
}
}
echo "\n";
}
inside child for loop内部子循环
replace if statement's condition from替换 if 语句的条件来自
if ((int) $c['ParentProfessionNum'] == $parent_id) {
with和
if ((int) $c['ParentProfessionNum'] == $p['id']) {
after analyzing your array, if objects with ProfessionName
"Production" and "Maintenance " are also parents than you should make "ParentProfessionNum"
empty or null分析数组后,如果ProfessionName
“Production”和“Maintenance”的对象也是父对象,则应将"ParentProfessionNum"
空或为空
"ParentProfessionNum" = ""
instead of代替
"ParentProfessionNum" = "1"
UPDATE (based on the question update)更新(基于问题更新)
with some object being both child and parent at a time,某个对象一次既是孩子又是父母,
you need more attributes (let's say is_parent
and is_child
) and then separate using child and parent using those attributes so some of the objects wil be on both arrays parent and child你需要更多的属性(比如is_parent
和is_child
),然后使用这些属性将 child 和 parent 分开,这样一些对象就会同时在 parent 和 child 数组上
foreach ($data as $job) {
if ($job['is_parent']) {
array_push($parents, $job);
}
if ($job['is_child']) {
array_push($childs, $job);
}
};
This looks like a logic problem.这看起来像是一个逻辑问题。
The first step should be to get all the parents.第一步应该是得到所有的父母。 Then loop through the array of parents and print all the children for that parent So,然后遍历父母数组并打印该父母的所有孩子所以,
// Get the unique values of parent Ids
$parentsIndex = array_unique(array_column($data, 'ParentProfessionNum'));
sort($parentsIndex);
//Loop through the parents
foreach ($parentsIndex as $key=>$p) {
// Skip Technique in parent
if("" != $p) {
// get the index for the original parent element in $data
$key = $p-1;
//always check if index exists
if (isset($data[$key]['ProfessionName'])) {
// Print Parent
echo $data[$key]['ProfessionName']."\n";
}
// Print children
foreach ($data as $c) {
//always check if index exists
if (isset($c['ParentProfessionNum'])) {
if ($c['ParentProfessionNum'] == $p ) {
echo " " . $c['ProfessionName']."\n";
}
}
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.