-2

I have a multidimensional array that is being built pulled from a file structure.

I am trying to use this array to create dynamic HTML tables based on the key for each item. The script I am using to pull the file structure is making each folder into the keys of the array.

So the array is outputting like this:

Array(
[english] => Array
    (
        [term1] => Array
            (
                [circ] => Array
                    (
                        [Unit1] => Array
                            (
                                [0] => file.zip
                            )

                        [Unit2] => Array
                            (
                                [0] => file.zip
                            )

                        [Unit3] => Array
                            (
                                [0] => file.zip
                            )

                        [Unit4] => Array
                            (
                                [0] => file.zip
                            )

                    )

                [type] => Array
                    (
                        [Unit1] => Array
                            (
                                [0] => file.zip
                            )

                        [Unit2] => Array
                            (
                                [0] => file.zip
                            )

                        [Unit3] => Array
                            (
                                [0] => file.zip
                           )

                        [Unit4] => Array
                            (
                                [0] => file.zip
                            )

                    )

            )

        [term2] => Array
            (
                [circ] => Array
                    (
                        [Unit1] => Array
                            (
                                [0] => file.zip
                            )

                        [Unit2] => Array
                            (
                                [0] => file.zip
                            )

                        [Unit3] => Array
                            (
                                [0] => file.zip
                            )

                        [Unit4] => Array
                            (
                                [0] => file.zip
                            )

                    )

                [type] => Array
                    (
                        [Unit1] => Array
                            (
                                [0] => file.zip
                            )

                        [Unit2] => Array
                            (
                                [0] => file.zip
                            )

                        [Unit3] => Array
                            (
                                [0] => file.zip
                            )

                        [Unit4] => Array
                            (
                                [0] => file.zip
                            )

                    )

            )
        )
    )

What I am trying to output is a page with a table that will use the keys something like this.

title = engligh
heading 1 = term1
heading 2 = circ
content 1 =
content 2 = Unit1
content 2 = Unit2
link = file.zip

{title}
<table class="table table-hover">
    <thead>
        <th>{heading1}</th>
        <th>{heading2}</th>
    </thead>
    <tbody>
        <tr>
            <td>{content1}</td>
            <td><a href="{link}">{content2}</a></td>
        </tr>
   </tbody>
</table>

I have not used PHP in over 5 years and to be honest was never very good at it in the first place, I have simply been thrown in the deep end at work and need some help to make this happen so any help given is very much appreciated!

Edit

So I have created a function that is pulling the first 2 level of keys from the array, however when I added in a third level it is just repeating the second level. I think recursive is more along the lines of what I need however I can not get my head around that.

function recursive(array $array){
    foreach($array as $key => $value){
        echo $key, '<br>';
        //If $value is an array.
        if(is_array($value)){
            //We need to loop through it.
            foreach($value as $key1 => $value1) {
                echo ' - ' . $key1, '<br>';
                //if $value is an array.
                if(is_array($value1)){
                    foreach ($value1 as $key2 => $value2) {
                        echo '   - ' . $key2, '<br>';
                    }
                }else{
                    echo ' - ' . $key1, '<br>';
                }
            }
        } else{
            //It is not an array, so print it out.
            echo $key, '<br>';
        }
    }
}

Can anyone tell me where I am going wrong with getting so many levels out of this function?

Simdash
  • 13
  • 1
  • 4
  • 1
    possible duplicate of [How to create a HTML Table from a PHP array?](http://stackoverflow.com/questions/4746079/how-to-create-a-html-table-from-a-php-array) – worldofjr Oct 21 '14 at 00:16
  • Use a `foreach($your_array as $key => $value)` and use a combination of the key and values to get the data you want. – scrowler Oct 21 '14 at 00:17
  • I understand that a foreach will need to be used, however I am getting stuck on what the combination of the key and values should be to be able to pull the data I need. – Simdash Oct 21 '14 at 00:27
  • just dump your keys and values to see what you have. go deeper if needed. `var_dump($value);` – gloomy.penguin Oct 21 '14 at 02:02
  • also... you never echo $value or $value1 if it isn't an array... just fyi. not sure if that leads to any confusion. – gloomy.penguin Oct 21 '14 at 02:04
  • Thanks gloomy.penguin I got it mostly working, good point on the the $value, that might solve my next problem! – Simdash Oct 21 '14 at 02:13

1 Answers1

0

I'm not sure how you wanted it printed. The example result is a little confusing. This should show you how to work through the array, though. I will see if I can get it a little closer to what you have up in the post but I don't get where the lowest level info is supposed to go exactly...

print "<style>table, td, tr {border: 1px solid black;}</style>"; 
print "<table>";

foreach($array as $class => $c_arr) {

   foreach($c_arr as $term => $t_arr) {

      foreach($t_arr as $circ_term => $ct_arr) {
         print "<tr><td>$class</td>";
         print "<td>$term</td>";
         print "<td>$circ_term</td>"; 

         foreach($ct_arr as $unit => $u_val) {
            print "<td>$unit = {$u_val[0]}</td>"; 
         }
         print "</tr>";
      }
   }
}
print "</table>";

yeah... based on your example of what you want... i still have no idea what it means.

title = engligh
heading 1 = term1
heading 2 = circ
content 1 = 
content 2 = Unit1
content 2 = Unit2
link = file.zip

so... what happens to type? and there are 4 file.zip's listed each time. and why is content 1 blank? no clue. you can also try this, though:

foreach($array as $class => $c_arr) {
   foreach($c_arr as $term => $t_arr) {
      print "title = $class<br/>";
      print "heading 1 = $term<br/>";
      foreach($t_arr as $circ_term => $ct_arr) {
         print "heading 2 = $circ_term<br/>"; 
         foreach($ct_arr as $unit => $u_val) {
            print "$unit = {$u_val[0]}<br/>"; 
         } 
      }
      print "<br/><br/>";
   }
   print "<br/><br/>";
} 

/*

title = english
heading 1 = term1
heading 2 = circ
Unit1 = file.zip
Unit2 = file.zip
Unit3 = file.zip
Unit4 = file.zip
heading 2 = type
Unit1 = file.zip
Unit2 = file.zip
Unit3 = file.zip
Unit4 = file.zip


title = english
heading 1 = term2
heading 2 = circ
Unit1 = file.zip
Unit2 = file.zip
Unit3 = file.zip
Unit4 = file.zip
heading 2 = type
Unit1 = file.zip
Unit2 = file.zip
Unit3 = file.zip
Unit4 = file.zip

*/
gloomy.penguin
  • 5,833
  • 6
  • 33
  • 59
  • Thank you and sorry, I have realised that what I asked for wasn't going to work in the overall scheme of what I was trying to do, I will have to go back to the drawing board and rethink what I need. – Simdash Oct 21 '14 at 06:06