up vote 2 down vote favorite

Hi guys,

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>
link|flag
You don't seem to use the values anywhere (e.g. 'b value'). Is that on purpose? – Phil Nov 19 '09 at 22:31
why is this community wiki? it is a valid programming question. you're loosing rep! – Byron Whitlock Nov 19 '09 at 22:35

6 Answers

up vote 3 down vote

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) {
    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>
link|flag
The indentation is a nice touch. – notJim Nov 19 '09 at 22:40
You could change the firs iterator for str_repeat(' ',$depth) – Sergi Nov 19 '09 at 22:42
up vote 0 down vote

how about

<pre>
<?php print_r($myArray); ?>
</pre>
link|flag
up vote 0 down vote
function block($a) {
    $ret = '<div>';
    if(is_array($a)) {
        foreach($a as $k => $v)
            $ret .= block($v); 
    } else {
        $ret .= $a;
    }
    $ret .= '</div>';
    return $ret;
}
link|flag
up vote 0 down vote
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);
link|flag
oops Sergi beats me... – Ben Nov 19 '09 at 22:38
up vote 0 down vote
function divArray($array){
    foreach($array as $key => $value){
       echo "<div>";
       echo $key;
       if(is_array($value)){
          divArray($value);
       }
       else{
         echo "$value";
      }
       echo "</div>";
  }
}
link|flag
You're only closing the divs when is not an array. – Sergi Nov 19 '09 at 22:37
oops! You're right. corrected. – GSto Nov 19 '09 at 22:43
up vote 0 down vote

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');
link|flag

Your Answer

 
or
never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.