0

我想从数组创建 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'
            )
        )
);
4

1 回答 1

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;

}

应该把你带到你需要去的地方。

于 2013-08-22T16:27:54.903 回答