Php多维数组html输出

时间:2013-08-22 16:03:48

标签: php html multidimensional-array output

我想从数组

创建html输出

但我无法完成代码。是否有人可以完成。

或者有更好的想法来编写这段代码吗?

$schema = array(
    0 => array(
        'tag' => 'div',
        'class' => 'lines',
        0 => array(
            'tag' => 'div',
            0 => array(
                'tag' => 'span',
                'key' => 'Product Name'
            ),
            'val' => 'Soap'
        )
        1 => array(
            'tag' => 'div',
            0 => array(
                'tag' => 'span',
                'key' => 'Product Name'
            ),
            'val' => 'Ball'
        )
    )
);

function get_output($schema){
    foreach($schema as $k => $v){
        if(is_array($v)){
            $v = get_output($v);
        }else{
            $info = '<$tag $att>$key</$tag>';
            if($k == 'tag'){ $info = str_replace('$tag',$v,$info); }
            if($k == 'class'){ $info = str_replace('$att','"'.$v.'" $att',$info); }
        }
    }
    return $info;
}

echo get_output($schema);

预期输出

<div class="lines">
    <div><span>Product Name</span>Soap</div>
    <div><span>Pruduct Name</span>Ball</div>
</div><!-- #lines -->

更新1

是否可以为后续数组创建相同的功能..

$schema = array(
    'div' => array(
        'class' => 'lines',
        'div' => array(
             'span' => array(
                'key' => 'Product Name'
            ),
            'val' => 'Soap'
        ),
        'div' => array(
             'span' => array(
                'key' => 'Product Name'
            ),
            'val' => 'Ball'
            )
        )
);

更新2

那个怎么样?

$schema = array(
    'div' => array(
        'class' => 'lines',
        'div' => array(
             'span' => array(
                'key' => 'Product Name'
            ),
            'val' => 'Soap'
        ),
        'layer' => array(
             'span' => array(
                'key' => 'Product Name'
            ),
            'val' => 'Ball'
            )
        )
);

1 个答案:

答案 0 :(得分:0)

可以像这样实现一个简单的递归树渲染器......

<?php

$schema = array(
array(
    'tag' => 'div',
    'class' => 'lines',
    array(
        'tag' => 'div',
         array(
            'tag' => 'span',
            'key' => 'Product Name'
        ),
        'val' => 'Soap'
    ),
     array(
        'tag' => 'div',
         array(
            'tag' => 'span',
            'key' => 'Product Name'
        ),
        'val' => 'Ball'
        )
    )
);

function get_output($schema){
    $tag = "";
    $attribs = array();
    $children = array();
    foreach($schema as $k => $v){        
        if(is_array($v)){
            $children[] = get_output($v);
        } else {
            switch($k){
                case "tag":
                    $tag = $v;
                    break;
                case "val":
                case "key":
                    $children[] = $v;
                    break;
                default:
                    $attribs[$k] = $v;
                    break;
            }
        }    
    }
    $str= "";
    if ($tag){
        $str = "<$tag";
        foreach($attribs as $k=>$v){
            $str.= " $k=\"".urlencode($v)."\"";
        }
        $str.= ">";
    }
    foreach($children as $child){
        $str.=$child;    
    }
    if ($tag){
        $str.= "</".$tag.">";
    }
    return $str;

}

echo get_output($schema);

输出

<div class="lines"><div><span>Product Name</span>Soap</div><div><span>Product Name</span>Ball</div></div>

如果不能发生重复的兄弟标记,请回答您的更新问题,然后重新实现您的get_output函数,如:

function get_output($schema, $tag = false){
    $attribs = array();
    $children = array();
    foreach($schema as $k => $v){        
        if(is_array($v)){
            $children[] = get_output($v, $k);
        } else {
            switch($k){
                case "val":
                case "key":
                    $children[] = htmlspecialchars($v);
                    break;
                default:
                    $attribs[$k] = $v;
                    break;
            }
        }    
    }
    echo $tag; print_r($children); print_r($attribs);
    $str= "";
    if ($tag){
        $str = "<$tag";
        foreach($attribs as $k=>$v){
            $str.= " $k=\"".urlencode($v)."\"";
        }
        $str.= ">";
    }
    foreach($children as $child){
        $str.=$child;    
    }
    if ($tag){
        $str.= "</".$tag.">";
    }
    return $str;

}

应该让你到达你需要的地方。