php多维数组递归到一个对象中

php多维数组递归到一个对象中,php,arrays,recursion,multidimensional-array,Php,Arrays,Recursion,Multidimensional Array,我有一个多维数组,我想把它创建成一个对象,这样我就可以输出xml和json 我很难弄清楚如何递归地做这件事。我看了很多多维的帖子,我可以在这里找到,但仍然卡住了 我做错了什么 class Dataset { public $name; public $attr = array(); public $children = array(); function __construct($name){ $this->name = $name;

我有一个多维数组,我想把它创建成一个对象,这样我就可以输出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>

这可能是将数据解析到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));

看看这个函数。*这个函数只访问叶节点*$jazzy数组的格式不正确-不知道这是你的代码的副本,但是应该引用键。如果我们可以复制/粘贴代码,会更容易提供帮助…数据也不一致-有时子项是一个或多个数组(节点),有时是一个字符串数组。您到底在哪里被卡住了?您已经显示了所需的输出,但没有显示当前的输出。
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));