1

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

3
  • you are missing the [ at the beginning? Commented Nov 25, 2019 at 20:11
  • question updated Commented Nov 25, 2019 at 20:12
  • ah ok, can i use other code or i should stay on your? Commented Nov 25, 2019 at 20:13

3 Answers 3

2

You have never set $parent_id... use instead $p['id'] or set $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

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";
}
Sign up to request clarification or add additional context in comments.

7 Comments

yes, it works but the output shows only the first parent-child
there is only one parent... only one element has "" as ParentProfessionNum, so there is only one parent, and the script show all the children of this parent "Technique Production Maintenance"
check "Maintenance operator" it's a child of Maintenance, so Maintenance is a child and also a parent
yes but you are adding to $parents only the ones that haven't a ParentProfessionNum value.
check if there this solution does what you want (edited the answere)
|
1

inside child for loop

replace if statement's condition from

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

"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

foreach ($data as $job) {
  if ($job['is_parent']) {
    array_push($parents, $job);
  } 
  if ($job['is_child']) {
    array_push($childs, $job);
  }
};

4 Comments

I did, but the problem is I need to loop through and show all child for every parent
so what is the output if you put the condition like (int) $c['ParentProfessionNum'] == $p['id']
the output shows first parent and his child Technique(parent) Production(child) Maintenance(child) but Production and Maintenance are parent too and have child I need to keep the loop and display all child
but as per the condition you put while separating child and parent (!$job['ParentProfessionNum']) will push only one item into parent because it only has empty ParentProfessionNum
0

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";
        }
      }
    }
  }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.