[SOLVED] Formatting Multidimensional Arrays

AoN

Internet Programmer
Joined
Aug 1, 2012
Posts
114
Ugh, I hate these brain farts. Long story short, I'm working on a customized, made-from-scratch, CMS to replace one of the websites currently used by the university I work for. For the sake of also catching up with the times, I'm working entirely in PDO, rather than just touching base here and there.

The PDO, I think, is not my problem, considering I have the menu array built from the database. Instead, I'm just having a lapse in intelligence in regards to how I can recursively format the array based upon what level of the array the information appears. Not sure why it is, but I'm just not getting any ideas on how to do this (I might've burnt myself out with the made-from-scratch session_handler() that adaptively uses cookies or $_GET with database validation. :S). So, I turn here! I'll give myself a break from the project for a few hours/days and see if I can think clearly afterwards, but I figure I can go ahead and ask anyways.

Here's the structure of the generated array:
Code:
Array
(
    [1] => Array
        (
            [id] => 1
            [pid] => 0
            [name] => Home
            [position] => 1
            [hidden] => 0
            [submenu] => Array
                (
                )
        )
    [2] => Array
        (
            [id] => 2
            [pid] => 0
            [name] => Student Resources
            [position] => 2
            [hidden] => 0
            [submenu] => Array
                (
                    [8] => Array
                        (
                            [id] => 8
                            [pid] => 2
                            [name] => Online Library
                            [position] => 1
                            [hidden] => 0
                            [submenu] => Array
                                (
                                )
                        )
                    [9] => Array
                        (
                            [id] => 9
                            [pid] => 2
                            [name] => Student Services
                            [position] => 2
                            [hidden] => 0
                            [submenu] => Array
                                (
                                )
                        )
                    [10] => Array
                        (
                            [id] => 10
                            [pid] => 2
                            [name] => MS Software
                            [position] => 3
                            [hidden] => 0
                            [submenu] => Array
                                (
                                )
                        )
                    [11] => Array
                        (
                            [id] => 11
                            [pid] => 2
                            [name] => Term Calendar
                            [position] => 4
                            [hidden] => 0
                            [submenu] => Array
                                (
                                )
                        )
                    [12] => Array
                        (
                            [id] => 12
                            [pid] => 2
                            [name] => Certification Tests
                            [position] => 5
                            [hidden] => 0
                            [submenu] => Array
                                (
                                )
                        )
                )
        )
    [3] => Array
        (
            [id] => 3
            [pid] => 0
            [name] => Key Contacts
            [position] => 3
            [hidden] => 0
            [submenu] => Array
                (
                    [13] => Array
                        (
                            [id] => 13
                            [pid] => 3
                            [name] => Student Services
                            [position] => 1
                            [hidden] => 0
                            [submenu] => Array
                                (
                                )
                        )
                    [14] => Array
                        (
                            [id] => 14
                            [pid] => 3
                            [name] => Online Support
                            [position] => 2
                            [hidden] => 0
                            [submenu] => Array
                                (
                                )
                        )
                    [15] => Array
                        (
                            [id] => 15
                            [pid] => 3
                            [name] => Online Career Advisors
                            [position] => 3
                            [hidden] => 0
                            [submenu] => Array
                                (
                                )
                        )
                    [16] => Array
                        (
                            [id] => 16
                            [pid] => 3
                            [name] => Online Administrative Staff 
                            [position] => 4
                            [hidden] => 0
                            [submenu] => Array
                                (
                                )
                        )
                    [17] => Array
                        (
                            [id] => 17
                            [pid] => 3
                            [name] => Online Financial Aid Representatives 
                            [position] => 5
                            [hidden] => 0
                            [submenu] => Array
                                (
                                )
                        )
                    [18] => Array
                        (
                            [id] => 18
                            [pid] => 3
                            [name] => Online Admissions Representatives 
                            [position] => 6
                            [hidden] => 0
                            [submenu] => Array
                                (
                                )
                        )
                    [19] => Array
                        (
                            [id] => 19
                            [pid] => 3
                            [name] => Program Directors
                            [position] => 7
                            [hidden] => 0
                            [submenu] => Array
                                (
                                )
                        )
                )
        )
    [4] => Array
        (
            [id] => 4
            [pid] => 0
            [name] => Student Awards
            [position] => 4
            [hidden] => 0
            [submenu] => Array
                (
                )
        )
    [5] => Array
        (
            [id] => 5
            [pid] => 0
            [name] => Tutorials
            [position] => 5
            [hidden] => 0
            [submenu] => Array
                (
                    [20] => Array
                        (
                            [id] => 20
                            [pid] => 5
                            [name] => Online Course Tutorial (WMV)
                            [position] => 1
                            [hidden] => 0
                            [submenu] => Array
                                (
                                )
                        )
                    [21] => Array
                        (
                            [id] => 21
                            [pid] => 5
                            [name] => Online Course Tutorial (SWF)
                            [position] => 2
                            [hidden] => 0
                            [submenu] => Array
                                (
                                )
                        )
                    [22] => Array
                        (
                            [id] => 22
                            [pid] => 5
                            [name] => CourseSmart eBook Tutorial
                            [position] => 3
                            [hidden] => 0
                            [submenu] => Array
                                (
                                )
                        )
                    [23] => Array
                        (
                            [id] => 23
                            [pid] => 5
                            [name] => Online Library Tutorial
                            [position] => 4
                            [hidden] => 0
                            [submenu] => Array
                                (
                                )
                        )
                    [24] => Array
                        (
                            [id] => 24
                            [pid] => 5
                            [name] => Entire Multimedia Library
                            [position] => 5
                            [hidden] => 0
                            [submenu] => Array
                                (
                                )
                        )
                )
        )
    [6] => Array
        (
            [id] => 6
            [pid] => 0
            [name] => eBook
            [position] => 6
            [hidden] => 0
            [submenu] => Array
                (
                    [25] => Array
                        (
                            [id] => 25
                            [pid] => 6
                            [name] => CourseSmart eBook Tutorial
                            [position] => 1
                            [hidden] => 0
                            [submenu] => Array
                                (
                                    [35] => Array
                                        (
                                            [id] => 35
                                            [pid] => 25
                                            [name] => Part 1
                                            [position] => 1
                                            [hidden] => 0
                                            [submenu] => Array
                                                (
                                                )
                                        )
                                    [36] => Array
                                        (
                                            [id] => 36
                                            [pid] => 25
                                            [name] => Part 2
                                            [position] => 2
                                            [hidden] => 0
                                            [submenu] => Array
                                                (
                                                )
                                        )
                                )
                        )
                    [26] => Array
                        (
                            [id] => 26
                            [pid] => 6
                            [name] => CourseSmart eBook Portal
                            [position] => 2
                            [hidden] => 0
                            [submenu] => Array
                                (
                                    [37] => Array
                                        (
                                            [id] => 37
                                            [pid] => 26
                                            [name] => Part 1
                                            [position] => 1
                                            [hidden] => 0
                                            [submenu] => Array
                                                (
                                                )
                                        )
                                    [38] => Array
                                        (
                                            [id] => 38
                                            [pid] => 26
                                            [name] => Part 2
                                            [position] => 2
                                            [hidden] => 0
                                            [submenu] => Array
                                                (
                                                )
                                        )
                                )
                        )
                    [27] => Array
                        (
                            [id] => 27
                            [pid] => 6
                            [name] => Benefits of Using eBooks
                            [position] => 3
                            [hidden] => 0
                            [submenu] => Array
                                (
                                )
                        )
                    [28] => Array
                        (
                            [id] => 28
                            [pid] => 6
                            [name] => New Feature - Offline access
                            [position] => 4
                            [hidden] => 0
                            [submenu] => Array
                                (
                                )
                        )
                    [29] => Array
                        (
                            [id] => 29
                            [pid] => 6
                            [name] => Mozilla Firefox download
                            [position] => 5
                            [hidden] => 0
                            [submenu] => Array
                                (
                                )
                        )
                )
        )
    [7] => Array
        (
            [id] => 7
            [pid] => 0
            [name] => Help
            [position] => 7
            [hidden] => 0
            [submenu] => Array
                (
                    [30] => Array
                        (
                            [id] => 30
                            [pid] => 7
                            [name] => Computer Requirements
                            [position] => 1
                            [hidden] => 0
                            [submenu] => Array
                                (
                                )
                        )
                    [31] => Array
                        (
                            [id] => 31
                            [pid] => 7
                            [name] => Survival Guide
                            [position] => 2
                            [hidden] => 0
                            [submenu] => Array
                                (
                                )
                        )
                    [32] => Array
                        (
                            [id] => 32
                            [pid] => 7
                            [name] => How to Access Your Student E-Mail
                            [position] => 3
                            [hidden] => 0
                            [submenu] => Array
                                (
                                )
                        )
                    [33] => Array
                        (
                            [id] => 33
                            [pid] => 7
                            [name] => 24/7 Technical Support
                            [position] => 4
                            [hidden] => 0
                            [submenu] => Array
                                (
                                )
                        )
                    [34] => Array
                        (
                            [id] => 34
                            [pid] => 7
                            [name] => Student Resource Services
                            [position] => 5
                            [hidden] => 0
                            [submenu] => Array
                                (
                                )
                        )
                )
        )
)

Here's how I need it to be formatted:
Code:
   $page .= '
   <tr>
    <td colspan="2">┣{NAME}</td>
    <td><input type="number" min="1" max="{LENGTH OF LEVEL OF ARRAY}" value="{POSITION}" /></td>
    <td><input type="checkbox"';
   if({HIDDEN} == 1) {
    $page .= ' checked="checked"';
   }
   $page .= ' /></td>
    <td><a href="?r=nav&o=rem&id={ID}">Remove?</a></td>
   </tr>';

For each additional level, ┃ would be added before ┣.

I'm running into two main issues. First, and the more annoying to me, I just can't seem to figure out how-to apply formatting based upon the level within the array. I'll be kicking myself for this for weeks after it's done. Secondly, and a little more based upon the first one since it's identification of array level, is retrieving the length of that array.

Basically, it should produce a table like:
Code:
┣Home                                    7   1   0   1
┣Student Resources                       7   2   0   1
┃┣Online Library                         5   1   0   1
┃┣Student Services                       5   2   0   1
┃┣MS Software                            5   3   0   1
┃┣Term Calendar                          5   4   0   1
┃┣Certification Tests                    5   5   0   1
┣Key Contacts                            7   3   0   1
┃┣Student Services                       7   1   0   1
┃┣Online Support                         7   2   0   1
┃┣Online Career Advisors                 7   3   0   1
┃┣Online Administrative Staff            7   4   0   1
┃┣Online Financial Aid Representatives   7   5   0   1
┃┣Online Admissions Representatives      7   6   0   1
┃┣Program Directors                      7   7   0   1
┣Student Awards                          7   4   0   1
┣Tutorials                               7   5   0   1
┃┣Online Course Tutorial (WMV)           5   1   0   1
┃┣Online Course Tutorial (SWF)           5   2   0   1
┃┣CourseSmart eBook Tutorial             5   3   0   1
┃┣Online Library Tutorial                5   4   0   1
┃┣Entire Multimedia Library              5   5   0   1
┣eBook                                   7   6   0   1
┃┣CourseSmart eBook Tutorial             5   1   0   1
┃┃┣Part 1                                2   1   0   1
┃┃┣Part 2                                2   2   0   1
┃┣CourseSmart eBook Portal               5   2   0   1
┃┃┣Part 1                                2   1   0   1
┃┃┣Part 2                                2   2   0   1
┃┣Benefits of Using eBooks               5   3   0   1
┃┣New Feature - Offline access           5   4   0   1
┃┣Mozilla Firefox download               5   5   0   1
┣Help                                    7   7   0   1
┃┣Computer Requirements                  5   1   0   1
┃┣Survival Guide                         5   2   0   1
┃┣How to Access Your Student E-Mail      5   3   0   1
┃┣24/7 Technical Support                 5   4   0   1
┃┣Student Resource Services              5   5   0   1

It angers me to think that this should be easy for me, but I can't change what it is. ^^'

Thank you all, ahead of time, for taking a look. ^^
 
So, a 1.5 hour break comes with some results. I got it to output, relatively, like I wanted. Problem is, it's not recursive to allow for adding additional levels of the menu. *sigh* My mind keeps screaming that this should be easy. ^^'

Here's what I came up with to do the output:
Code:
 foreach($nav as $value) {
  echo '┣' . $value['name'] . '   ' . sizeof($nav) . '   ' . $value['position'] . '   ' . $value['hidden'] . '   ' . $value['id'] . '<br />';
  foreach($value['submenu'] as $value2) {
   echo '┃┣' . $value2['name'] . '   ' . sizeof($value['submenu']) . '   ' . $value2['position'] . '   ' . $value2['hidden'] . '   ' . $value2['id'] . '<br />';
   foreach($value2['submenu'] as $value3) {
    echo '┃┃┣' . $value3['name'] . '   ' . sizeof($value2['submenu']) . '   ' . $value3['position'] . '   ' . $value3['hidden'] . '   ' . $value3['id'] . '<br />';
   }
  }
 }

So long as the server doesn't reboot again, you can see it output at: http://68.10.31.250:8080/collegeonline/acp/navtest.php

As you can see, it is entirely dependent upon my adding a new copy of the foreach() loop for each tier of the array, definitely not what I want. If anyone has an idea, I want to kick myself for failing on what should be an easy function. ^^'
 
I have not looked at your code nor can I test anything as I am currently mobile but...

Why worry about figuring out the level? Just write a recursive function call and pass in a string prefix. If you have a sub array call the same function on that but append a bar onto the beginning.

This in and of itself should solve the leveling problem should it not?
 
A weekend of delivering pizzas doesn't seem to have helped much.

Here's what I got:
Code:
 genList(getMenu($result));
 function genList($nav = null, $parent = 0, $output = '') {
  foreach($nav as $i => $item) {
   echo $output . '┣' . $item['name'] . '   ' . sizeof($nav) . '   ' . $item['position'] . '   ' . $item['hidden'] . '   ' . $item['id'] . '<br />';
   if($item['pid'] == $parent) {
    if($parent != 0) {
     $output = '┃';
    }
    genList($item['submenu'], $item['id'], $output);
   }
  }
 }

The getMenu() function is what returns the array I previously posted. The genList() function is recursively generating the actual list displayed on the page, check in there is more than 0 entries for submenu and then running the function on the submenu if there is.

My problem, at this point, is that the thing doesn't distinguish between the first level sub and anything beyond, so everything displays on the first sub level, even if it's a second sub.

Maybe this is an indication that I need to find a new pursuit in life. :(
 
Change
Code:
$output = '┃';

to

Code:
$output += '┃';

and the deed should be done?

And don't beat yourself up too much, I've spend far longer trying to solve easier problems.... (sometimes with no luck)
 
Alright, mostly got it. Only "real" problem is just a formatting thing, sorta.

So, I updated that genList() function to:
Code:
 function genList($nav = null, $parent = 0, $pre = '┣') {
  foreach($nav as $i => $item) {
   if($item['pid'] == $parent) {
    if($parent != 0) {
     if($item['position'] == 1) {
      $pre = '┃' . $pre;
     } else if($item['position'] == sizeof($nav)) {
      $pre = str_replace('┣', '┗', $pre);
     }
    }
    $output = $pre;
    echo $output . $item['name'] . '   ' . sizeof($nav) . '   ' . $item['position'] . '   ' . $item['hidden'] . '   ' . $item['id'] . '<br />';
    genList($item['submenu'], $item['id'], $pre);
   }
  }
 }

This gets all the spacing correct, and adds a little extra to make it "prettier" in appearance. The outstanding portion is in regards to the end of the list. With this fucntion, it displays like:
Code:
┣Help   7   7   0   7
┃┣Computer Requirements   5   1   0   30
┃┣Survival Guide   5   2   0   31
┃┣How to Access Your Student E-Mail   5   3   0   32
┃┣24/7 Technical Support   5   4   0   33
┃┗Student Resource Services   5   5   0   34

For the sake of cleanliness, I'd like to make it appear like:
Code:
┣Help   7   7   0   7
 ┣Computer Requirements   5   1   0   30
 ┣Survival Guide   5   2   0   31
 ┣How to Access Your Student E-Mail   5   3   0   32
 ┣24/7 Technical Support   5   4   0   33
 ┗Student Resource Services   5   5   0   34

Any ideas?
 
Yall really should consider the whole timeout on editing previous posts. :P

It ain't pretty, but I made it happen:
Code:
 function genList($nav = null, $parent = 0, $pre = '┣', $last = 0) {
  foreach($nav as $i => $item) {
   if($item['pid'] == $parent) {
    if($parent != 0) {
     if($item['position'] == 1) {
      if($last == 1) {
       $pre = ' ' . $pre;
       $last = 0;
      } else {
       $pre = '┃' . $pre;
      }
     } else if($item['position'] == sizeof($nav)) {
      $pre = str_replace('┣', '┗', $pre);
     }
    }
    if($item['position'] == sizeof($nav) && $parent == 0) {
     $pre = '┗';
    }
    echo $pre . $item['name'] . '   ' . sizeof($nav) . '   ' . $item['position'] . '   ' . $item['hidden'] . '   ' . $item['id'] . '<br />';
    if($item['position'] == sizeof($nav) && $parent == 0) {
     $pre = '┣';
     $last = 1;
    }
    genList($item['submenu'], $item['id'], $pre, $last);
   }
  }
 }

Added another variable to the function to track whether or not it was the last entry of the array while testing that the parent was 0 (base-level).

Thanks for taking a look. Hopefully, I won't run into anymore trouble. Now that I got this basic list working, it should just be a matter of applying my formatting. ^^
 
Sorry you're right my suggestion wouldn't work :lolg:

I was thinking it was more recursive not iterative and I only wanted the extra "|" passed down to the children :lol:

As per timeout, it is set the way it is so that spam bots won't come back and edit their posts.

Since you are staff though it shouldn't really apply...

Bug John(jcgriff2) about it, he is very particular about how user groups are set up and I don't want to change it. :smile9:

Maybe one of the other admin are braver than I am...
 
lol, I'm not staff. I was only given a special title. xD

As for the function not being very recursive, I'm outta practice, what more can I say?
 

Has Sysnative Forums helped you? Please consider donating to help us support the site!

Back
Top