Php多维数组html输出


Php multidimensional array html 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'
            )
        )
);

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

<?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;
}

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