4

I'm looking to write a recursive php function that would call a function to generate nested HTML blocks ( not necessarily only DIVs ). So for example, for the following array:

$a = array(
    'b' => 'b value',
    'c' => 'c value',
    'd' => array(
        'd1' => array(
            'd12' = 'd12 value'
        ),
        'd2' => 'd2 value'
    ),
    'e' => 'e value'
);

and the following function

function block( $key ) {
    return '<div>'.$key.'</div>';
}

would result into

<div>
    key - b
</div>
<div>
    key - c
</div>
<div>
    key - d
    <div>
        key - d1
        <div>
            key - d12
        </div>
    </div>
    <div>
        key - d2
    </div>
</div>
<div>
    key - e
</div>
2
  • You don't seem to use the values anywhere (e.g. 'b value'). Is that on purpose? Commented Nov 19, 2009 at 22:31
  • 1
    why is this community wiki? it is a valid programming question. you're loosing rep! Commented Nov 19, 2009 at 22:35

6 Answers 6

8

Excuse the crude formatting and the very crude way of indenting for you, but it should work as you've formatted above. Notice the use of in_array(...)

CODE

nestdiv($a);

function nestdiv($array, $depth = 0) {
    $indent_str = str_repeat(" ", $depth);

    foreach ($array as $key => $val) {
        print "$indent_str<div>\n";
        print "${indent_str}key - $key\n";
        if (is_array($val))
            nestdiv($val, ($depth+1));
        print "$indent_str</div>\n";
    }
}

OUTPUT

<div>
key - b
</div>
<div>
key - c
</div>
<div>
key - d
    <div>
    key - d1
        <div>
        key - d12
        </div>
    </div>
    <div>
    key - d2
    </div>
</div>
<div>
key - e
</div>
1
  • You could change the firs iterator for str_repeat(' ',$depth) Commented Nov 19, 2009 at 22:42
1

how about

<pre>
<?php print_r($myArray); ?>
</pre>
0
function block($a) {
    $ret = '<div>';
    if(is_array($a)) {
        foreach($a as $k => $v)
            $ret .= block($v); 
    } else {
        $ret .= $a;
    }
    $ret .= '</div>';
    return $ret;
}   
0
function block($array)
{
    $s = '';
    foreach ($array as $key=>$value)
    {
        $s .= '<div>' . $key ;
        if (is_array($value))
            $s .= block($value);
        else
            $s .= $value;
        $s .= '</div>';
    }
    return $s;
}
echo block($a);
0
0
function divArray($array){
    foreach($array as $key => $value){
       echo "<div>";
       echo $key;
       if(is_array($value)){
          divArray($value);
       }
       else{
         echo "$value";
      }
       echo "</div>";
  }
}
1
  • You're only closing the divs when is not an array. Commented Nov 19, 2009 at 22:37
0

The other answers did not take into account the fact that he wants to be able to give the block() function as parameter :

function toNested($array, $blocFunc) {
    $result = '';

    foreach ($array as $key => $value) {
        if is_array($value)
            $nestedElement = toNested($value, $blocFunc);
        else
            $nestedElement = $blocFunc($key)

        $result .= $nestedElement;
    }

    return $result;
}

echo toNested($a, 'block');

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.