简体   繁体   English

PHP多维数组HTML输出

[英]Php multidimensional array html output

I want to create html output from array 我想从数组创建html输出

But I could not completed the code. 但是我无法完成代码。 Are there any one who can complete. 有谁能完成。

Or are there any better idea to write this code? 还是有更好的主意来编写此代码?

$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);

Expected Output is 预期输出为

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

UPDATE 1 更新1

Is it possible to create same function for following array.. 是否可以为以下数组创建相同的功能。

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

UPDATE 2 更新2

What about that one? 那一个呢?

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

A simple recursive tree renderer can be implemented like this... 可以像这样实现一个简单的递归树渲染器。

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

Outputs 产出

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

To answer your updated question if duplicate sibling tags cannot happen then reimplementing your get_output function like: 要回答更新的问题,如果不能发生重复的同级标签,请重新实现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;

}

Should get you where you need to be. 应该可以让您到达需要的地方。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM