0

There is the following array.

  $arrTmp = array();
  $arrTmp = array(
  array('id' => 206,
  'BoardName' => 'Schedule',
  'pid' => 196),

  array('id' => 145,
  'BoardName' => 'testboard3',
  'pid' => 196),

  array('id' => '197',
  'BoardName' => 'testboard',
  'pid' => 196),     
 
  array('id' => '198',
  'BoardName' => 'hawi',
  'pid' => 197),
 
  array('id' => '199',
  'BoardName' => 'hawi2',
  'pid' => 197)    
     
);
Array
(
    [0] => Array
        (
            [id] => 206
            [BoardName] => Schedule
            [pid] => 196
        )
    [1] => Array
        (
            [id] => 145
            [BoardName] => testboard3
            [pid] => 196
        )


    [2] => Array
        (
            [id] => 197
            [BoardName] => testboard
            [pid] => 196
        )

    [3] => Array
        (
            [id] => 198
            [BoardName] => hawi
            [pid] => 197
        )

    [4] => Array
        (
            [id] => 199
            [BoardName] => hawi2
            [pid] => 197
        )     
  );


"Pid" is the parent of "id". An array with "pid" 197 enters a sub-category of an array with id "197".

If there is no array of id values as pid, it is displayed like an array of 206 or 145 id. 206 or 145 is an example and does not necessarily have to match that value.

Examples of pid are "197" or "196", and there are many types of pid and id in the DB.

I want to make this Array as follows.

    $arrTmp2 = array(
    array('id' => 206,
    'title' => 'Schedule',
    'pid' => 196),
    
    array('id' => '197',
    'title' => 'testboard',
    'pid' => 196,
          'sub' => array(
              
            'hawi' => array
              (
               'title' => 'hawi',
              'id' => 198       
                ),
            'hawi2' => array
              (
               'title' => 'hawi2',
              'id' => 199       
                )
             
        )
    )
);

Array
(
    [0] => Array
        (
            [id] => 206
            [title] => Schedule
            [pid] => 196
        )

    [1] => Array
        (
            [id] => 197
            [title] => testboard
            [pid] => 196
            [sub] => Array
                (
                    [hawi] => Array
                        (
                            [title] => hawi
                            [id] => 198
                        )

                    [hawi2] => Array
                        (
                            [title] => hawi2
                            [id] => 199
                        )

                )

        )


What should I do? I ask for help.

foreach ($arrTmp as $key => $value) {


        
}
1
  • 1
    That is not really much of an attempt at solving this yourself! Commented Mar 7, 2022 at 15:08

1 Answer 1

0

What you describe looks like a tree data structure. You might find there the implementation you need.

Sign up to request clarification or add additional context in comments.

1 Comment

What you write is really just a comment,

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.