0

I have the following PHP array.

Array
(
 [168] => Array
    (
        [link] => asdfasdf
        [children] => Array
            (
                [239] => Array
                    (
                        [link] => tascatestlalal
                        [children] => Array
                            (
                            )
                    )
                [240] => Array
                    (
                        [link] => otrotestttt
                        [children] => Array
                            (
                            )
                    )
            )
    )
[229] => Array
    (
        [link] => Sub-task ex
        [children] => Array
            (
            )
    )

[230] => Array
    (
        [link] => Sub-task test
        [children] => Array
            (
                [231] => Array
                    (
                        [link] => tasktest1
                        [children] => Array
                            (
                            )
                    )
                [232] => Array
                    (
                        [link] => tasktest 2
                        [children] => Array
                            (
                                [233] => Array
                                    (
                                        [link] => tasktest 5
                                        [children] => Array
                                            (
                                                [235] => Array
                                                    (
                                                        [link] => tasca235
                                                        [children] => Array
                                                            (
                                                            )
                                                    )
                                            )
                                    )
                            )
                    )
                [234] => Array
                    (
                        [link] => tasca234
                        [children] => Array
                            (
                            )
                    )
            )
    )
 )  

and I need to convert it into this

<table>
 <tr>
 <td>
 Sub-task
 </td>
 </tr>
 <tr>
 <td>
 --Tasktest1
  </td>
  </tr>
  <tr>
  <td>
  --tasktest 2
  </td>
  </tr>
  <tr>
  <td>
  ---tasktest 5
  </td>
  </tr>
  <tr>
  <td>
  ----tasca235
  </td>
  </tr>
  <tr>
  <td>
  --tasca234
  </td>
  </tr>
 </table>

this I already know how to do it with a list with this function, but cant seen to modify this function to transform it into the table example above :( . some help will be greatly appreciated

function ArrayToHTMLList($arr) {
$str = "<ul class='tasklist'>";
foreach($arr as $key => $value) {

        if((!empty($value))){

          $str .="<li>";

            if(is_array($value))
                  $str .= ArrayToHTMLList($value);
            else                  
                $str .= $value;

          $str .= "</li>";         
        }

}

$str .= "</ul>";
return $str;
}
1
  • You'd get a lot more takers if you posted proper php code for creating an array instead of the result of var_dump( $arr ); i.e. $x = var_export( $arr, 1); echo $x; Commented Jun 1, 2011 at 17:47

1 Answer 1

1
function nestedArrayToFlatList($node, $indent) {
  $str = '';

  if (isset($node['link'])) {
    $str .= '<tr><td>' . $indent . $node['link'] . '</td></tr>';
  }

  if (isset($node['children']) && count($node['children']) > 0) {
    foreach ($node['children'] as $child) {
      $str .= nestedArrayToFlatList($child, '--' . $indent);
    }
  }

  return $str;
}


$myList = '<table>' . nestedArrayToFlatList($myMassiveArray, '') . '</table>';
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.