【问题标题】:Php multidimensional array html outputphp多维数组html输出
【发布时间】:2013-08-22 16:03:47
【问题描述】:

我想从数组创建 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 html multidimensional-array output


    【解决方案1】:

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

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

    应该会带你去你需要去的地方。

    【讨论】:

    • Orangepill 这是闪电般的答案。这是完美的解决方案.. 是否可以从 UPDATED 数组结构创建相同的输出?有名字
    • @DenizPorsuk 您的数组在更新后的示例中不起作用...有两个名为 div 的键。
    • 你完全正确。我更改了代码。是否适用于 UPDATE 2
    • 我希望数组尽可能小。
    • @DenizPorsuk 你的第一个方法是最明智的。给定层作为关联键,如何知道它应该是一个 div?
    猜你喜欢
    • 1970-01-01
    • 2011-01-28
    • 1970-01-01
    • 1970-01-01
    • 2021-02-19
    • 1970-01-01
    • 2017-04-07
    • 1970-01-01
    相关资源
    最近更新 更多