php多维数组递归到对象

时间:2017-07-28 16:47:07

标签: php arrays recursion multidimensional-array

我有一个多维数组,我想创建一个对象,然后我可以输出为xml和json。

我很难理解如何以递归方式执行此操作。我看过很多我在这里可以找到的多维帖子,但仍然卡住了。

我做错了什么?

class Dataset
{
    public $name;
    public $attr = array();
    public $children = array();

    function __construct($name){
        $this->name = $name;
    }

    function addAttr($attr){
        $this->attr[] = $attr;
    }

    function addChildren($children){
        $this->children[] = $children;
    }
}



$jazzy = Array(
    name => 'aaa',
    attr => Array(
        id => 123
    ),
    children => Array(
        Array(
            name => 'name',
            attr => Array(),
            children => Array(
                'www'
            ),
        ),
        Array(
            name => 'websites',
            attr => Array(),
            children => Array(
                Array(
                    name => 'website',
                    attr => Array(
                        id => 456,
                        class => 'boom'

                    ),
                    children => Array(
                        Array(
                            name => 'url',
                            attr => Array(),
                            children => Array(
                                'www.test.com'
                            )
                        )
                    )
                ),
                Array(
                    name => 'website',
                    attr => Array(
                        id => 123,
                        class => "boom"
                    ),
                    children => Array(
                        Array(
                            name => 'url',
                            attr => Array(),
                            children => Array(
                                'www.example.com'
                            )
                        )
                    )
                )
            )
        )
    )
);

我希望创建此输出

<aaa id="123">
    <name>www</name>
    <websites>
        <website id='456' class="boom">
            <url>www.test.com</url>
        </website>
        <website id='123 class="boom">
            <url>www.example.com</url>
        </website>
    </websites>
</aaa>

我的代码

function arrayToDataset($array, $node){

    foreach ($array as $key => $value) {
        $name = $array['name'];
        $attr = $array['attr'];
        $children = $array['children'];

        if($key == "name"){
            $name = $value;
            $node->addName($name);
        }
        elseif($key == "attr"){
            $attr = $value;

        $node->addAttr($attr);
        }

        elseif($key == "children")
        {
            $children = $value;
            $newNode = new Dataset();

            foreach($children as $k => $v)
            {
                $newNode = $node->addChildren($v);
            }
            return arrayToDataset($children, $newNode);
        }
    }
}

$node = new Dataset();
$thing = arrayToDataset($jazzy, $node);
print_r($thing);

1 个答案:

答案 0 :(得分:1)

这可能是一种将数据解析为DataSet对象的方法,然后您可以使用该方法以xml或json等其他格式输出。可能有更简单的方法来做到这一点......

class Dataset
{
    public $name;
    public $attr = array();
    public $children = array();
    public $url = array();

    function __construct($name)
    {
        $this->name = $name;
    }

    function addAttr($attr, $value)
    {
        $this->attr[$attr] = $value;
    }

    function addChild(DataSet $child)
    {
        $this->children[] = $child;
    }

    function addUrl($url) {
        $this->url[] = $url;
    }

    static function makeNode($array)
    {
        // $array needs to have the required elements
        $node = new DataSet($array['name']);
        if (isset($array['attr'])) {
            foreach ($array['attr'] as $k => $v) {
                if (is_scalar($v)) {
                    $node->addAttr($k, $v);
                }
            }
        }
        if (isset($array['children']) && is_array($array['children'])) {
            foreach ($array['children'] as $c) {
                if(is_scalar($c)) {
                    $node->addUrl($c);
                } else {
                    $node->addChild(self::makeNode($c));
                }
            }
        }
        return $node;
    }

}

print_r(Dataset::makeNode($jazzy));