Skip to content
Advertisement

How to create multidimensional array unlimited depth array with parent and child came from single table

Im trying to create multi-dimesional array with unlimited depth. I select the data from the database and check it if the field ‘isArray’ is true, meaning this column is a parent then I tried to make a loop to make it look for its child 'parent_id' => $row->id.

I’m expecting output like this.

array(
[0]=> array(
    'id' => '29',
    'section' => '',
    'sorting' =>'',
    'title' => 'POWERS OF THE BANKO SENTRAL',
    'pdf_file' => '',
    'content' => '',
    'parent_id' => '0',
    'isArray' => array(
            [0] => array(
                'id' => '30',
                'section' => '001',
                'sorting' => '',
                'title' => 'Examination by the Bangko Sentral',
                'pdf_file' => 'NRBSL CHRISTMAS PARTY RAFFLE WINNERS.pdf',
                'parent_id' => '29',
                'isArray' => 0,
            ),
            [1] => array(
                'id' => '31',
                'section' => '002',
                'sorting' => '',
                'title' => 'Supervisory Enforcement',
                'pdf_file' => 'APL-Form1.pdf'
                'parent_id' => '29',
                'isArray' => 0
            )
        ),
    ),
[1]=> array(
    [0] => array(
        'id' => '32',
        'section' => '',
        'sorting' => '',
        'title' => 'A. Risk Management',
        'pdf_file' => '',
        'content' => '',
        'parent_id' => '0',
        'isArray' => array(
            [0] => array(
                'id' => '33',
                'section' => '911',
                'sorting' => '',
                'title' => 'RISK MANAGEMENT',
                'pdf_file' => '',
                'content' => '',
                'parent_id' => '32',
                'isArray' => array(
                    [0] => array(
                        'id' => '34',
                        'section' => 'ASDF',
                        'sorting' => '',
                        'title' => 'ASDFSDF',
                        'pdf_file' => '',
                        'content' => '',
                        'parent_id' => '33',
                        'isArray' = array()
                        )
                    )
              )
            )
        )
    )
)
    

And the data I get from the database is this enter image description here

I came up with this code:

public function findParentsParent($result) {
            global $subs;
            foreach ($result as $row) {
                $isArray = filter_var($row->isArray, FILTER_VALIDATE_BOOLEAN);
                if ($isArray) {
                    $subs[][$row->parent_id] = $row;
                    $where = ['parent_id' => $row->id];
                    $result = $this->my_model->select_where('tbl_policies', $where, 'sorting, section');
                    if(!empty($result))
                        $this->findParentsParent($result);
                    //return array_reverse($subs);
                } else {
                    $subs[] = $row;
                }
            }
            return array_reverse($subs);
        }

But I ended up with this array:

array(6) {
  [0]=>
  object(stdClass)#44 (11) {
    ["id"]=>
    string(2) "30"
    ["section"]=>
    string(3) "001"
    ["sorting"]=>
    string(0) ""
    ["title"]=>
    string(33) "Examination by the Bangko Sentral"
    ["pdf_file"]=>
    string(41) "NRBSL CHRISTMAS PARTY RAFFLE WINNERS1.pdf"
    ["content"]=>
    string(0) ""
    ["parent_id"]=>
    string(2) "29"
    ["isArray"]=>
    string(1) "0"
    ["uploaded_by"]=>
    string(1) "6"
    ["created_at"]=>
    string(19) "2022-03-03 11:46:06"
    ["updated_at"]=>
    string(19) "2022-03-03 11:46:06"
  }
  [1]=>
  object(stdClass)#43 (11) {
    ["id"]=>
    string(2) "31"
    ["section"]=>
    string(3) "002"
    ["sorting"]=>
    NULL
    ["title"]=>
    string(30) "Supervisory Enforcement Policy"
    ["pdf_file"]=>
    string(13) "APL-Form1.pdf"
    ["content"]=>
    string(0) ""
    ["parent_id"]=>
    string(2) "29"
    ["isArray"]=>
    string(1) "0"
    ["uploaded_by"]=>
    string(1) "6"
    ["created_at"]=>
    string(19) "2022-03-03 13:19:27"
    ["updated_at"]=>
    string(19) "2022-03-03 13:19:27"
  }
  [2]=>
  array(1) {
    [0]=>
    object(stdClass)#40 (11) {
      ["id"]=>
      string(2) "29"
      ["section"]=>
      string(0) ""
      ["sorting"]=>
      string(0) ""
      ["title"]=>
      string(28) "POWERS OF THE BANGKO SENTRAL"
      ["pdf_file"]=>
      string(0) ""
      ["content"]=>
      string(0) ""
      ["parent_id"]=>
      string(1) "0"
      ["isArray"]=>
      string(1) "1"
      ["uploaded_by"]=>
      string(1) "6"
      ["created_at"]=>
      string(19) "2022-03-03 11:45:25"
      ["updated_at"]=>
      string(19) "2022-03-03 11:45:25"
    }
  }
  [3]=>
  array(1) {
    [33]=>
    object(stdClass)#42 (11) {
      ["id"]=>
      string(2) "34"
      ["section"]=>
      string(4) "ASDF"
      ["sorting"]=>
      NULL
      ["title"]=>
      string(7) "ASDFSDF"
      ["pdf_file"]=>
      string(0) ""
      ["content"]=>
      string(0) ""
      ["parent_id"]=>
      string(2) "33"
      ["isArray"]=>
      string(1) "1"
      ["uploaded_by"]=>
      string(1) "6"
      ["created_at"]=>
      string(19) "2022-03-04 09:29:37"
      ["updated_at"]=>
      string(19) "2022-03-04 09:29:37"
    }
  }
  [4]=>
  array(1) {
    [32]=>
    object(stdClass)#41 (11) {
      ["id"]=>
      string(2) "33"
      ["section"]=>
      string(3) "911"
      ["sorting"]=>
      NULL
      ["title"]=>
      string(15) "RISK MANAGEMENT"
      ["pdf_file"]=>
      string(0) ""
      ["content"]=>
      string(0) ""
      ["parent_id"]=>
      string(2) "32"
      ["isArray"]=>
      string(1) "1"
      ["uploaded_by"]=>
      string(1) "6"
      ["created_at"]=>
      string(19) "2022-03-04 09:29:18"
      ["updated_at"]=>
      string(19) "2022-03-04 09:29:18"
    }
  }
  [5]=>
  array(1) {
    [0]=>
    object(stdClass)#39 (11) {
      ["id"]=>
      string(2) "32"
      ["section"]=>
      NULL
      ["sorting"]=>
      NULL
      ["title"]=>
      string(18) "A. Risk Management"
      ["pdf_file"]=>
      string(0) ""
      ["content"]=>
      string(0) ""
      ["parent_id"]=>
      string(1) "0"
      ["isArray"]=>
      string(1) "1"
      ["uploaded_by"]=>
      string(1) "6"
      ["created_at"]=>
      string(19) "2022-03-04 09:28:41"
      ["updated_at"]=>
      string(19) "2022-03-04 09:28:41"
    }
  }
}

Advertisement

Answer

Credits to Recursive function to generate multidimensional array from database result

I solved my problem with this code

public function buildTree(array $elements, $parentId = 0) {
            $branch = array();
            foreach ($elements as $element) {
                if ($element['parent_id'] == $parentId) {
                    $children = $this->buildTree($elements, $element['id']);
                    $isArray = filter_var($element['isArray'], FILTER_VALIDATE_BOOLEAN);
                    if ($isArray) {
                        $element['isArray'] = $children;
                    }
                    $branch[] = $element;
                }
            }
            return $branch;
        }
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement