G
G
gistol2015-11-05 17:56:05
PHP
gistol, 2015-11-05 17:56:05

How to add an element at the right place to the parent in the array?

Greetings!
Help solve the problem. I'm trying to create an array that contains tasks. Each task can have a parent element.
I want the array to look like a hierarchical tree with each child element inside the parent element.

Array
(
    [285] => Array
        (
            [id] => 285
            [name] => Подготовить КП
            [childid] => Array
                (
                    [288] => Array
                        (
                            [id] => 288
                            [name] => Протокол
                            [childid] => Array
                                (
                                    [286] => Array
                                        (
                                            [id] => 286
                                            [name] => Подготовить договор
                                        )

                                )

                        )

                )

        )

    [289] => Array
        (
            [id] => 289
            [name] => 234234
        )

    [298] => Array
        (
            [id] => 298
            [name] => Посчитать ставки ст.Элисенваара  -  ст. Окуловка ст. Приозерск - ст. Окуловка (группа 21 и более, щебень в полувагонах и думпкарах)   Просим Вас подобрать перевалку (тупик) для приёма нерудных материалов в районе д.Лысково Нижегородской области. Перевалка
        )

)

$allready_added_child_tasks = array();
      $SmarttasksObjects = new Object\Projecttask\Listing();
      $SmarttasksObjects->setOrderKey("date");
      $SmarttasksObjects->setOrder("desc");
      foreach ($SmarttasksObjects as $task) {
        if (!in_array($task->getId(), $allready_added_child_tasks)) {
          $taskChildId = $task->getChildid();
          $taskmemeber = $task->getMemberId();
          if (isset($taskmemeber) && $taskmemeber) {
            $exp_array = explode(',', $taskmemeber);
            foreach ($exp_array as $SmarttasksIdObject) {
              $smarttasks_array_member[] = $SmarttasksIdObject;
            }
            if (isset($smarttasks_array_member)) {
              foreach ($smarttasks_array_member as $smartask_member) {
                $SmarttasksObjects_array[$task->getId()]['id'] = $task->getId();
                $SmarttasksObjects_array[$task->getId()]['name'] = $task->getName();
                if ($taskChildId > 0) {
                  $childTask = Object_Projecttask::getById($taskChildId);
                  $SmarttasksObjects_array[$task->getId()]['childid'][$task->getChildid()]['id'] = $childTask->getId();
                  $SmarttasksObjects_array[$task->getId()]['childid'][$task->getChildid()]['name'] = $childTask->getName();
                  $child_childtaskid = $childTask->getChildId();
                  if ($child_childtaskid > 0) {
                    $child_childTask = Object_Projecttask::getById($child_childtaskid);
                    $SmarttasksObjects_array[$task->getId()]['childid'][$task->getChildid()]['childid'][$child_childtaskid]['id'] = $child_childTask->getId();
                    $SmarttasksObjects_array[$task->getId()]['childid'][$task->getChildid()]['childid'][$child_childtaskid]['name'] = $child_childTask->getName();
                  }
                  $allready_added_child_tasks[] = $childTask->getId();
                }
              }
            }
            
          }
        }
      }

I can do it manually, but I understand that this is not quite right.
if ($taskChildId > 0) {
                  $childTask = Object_Projecttask::getById($taskChildId);
                  $SmarttasksObjects_array[$task->getId()]['childid'][$task->getChildid()]['id'] = $childTask->getId();
                  $SmarttasksObjects_array[$task->getId()]['childid'][$task->getChildid()]['name'] = $childTask->getName();
                  $child_childtaskid = $childTask->getChildId();
                  if ($child_childtaskid > 0) {
                    $child_childTask = Object_Projecttask::getById($child_childtaskid);
                    $SmarttasksObjects_array[$task->getId()]['childid'][$task->getChildid()]['childid'][$child_childtaskid]['id'] = $child_childTask->getId();
                    $SmarttasksObjects_array[$task->getId()]['childid'][$task->getChildid()]['childid'][$child_childtaskid]['name'] = $child_childTask->getName();
                  }
                  $allready_added_child_tasks[] = $childTask->getId();
                }

How to make a more elegant solution?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Denis, 2015-11-05
@prototype_denis

PHP. How to form a dynamic multidimensional array?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question