0

I have a multidimensional array that i would like to create into an object so I can then output both as xml and json.

I am having difficulty getting my head around how to do this recursively. I have looked at many multidimensional posts I can find on here but am still stuck.

What am I doing wrong?

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

I am looking to create this output

<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>

My code

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);
5
  • take a look at the array_walk_recursive() function. Commented Jul 28, 2017 at 16:51
  • * this function only visits leaf nodes * Commented Jul 28, 2017 at 16:57
  • the $jazzy array isn't properly formatted - don't know iif this is a copy of your code, but the keys should be quoted. It would be easier to help if we could copy/paste the code... Commented Jul 28, 2017 at 18:27
  • The data is inconsistent also - sometimes children is an array or arrays (nodes) and sometimes an array of strings. Commented Jul 28, 2017 at 18:41
  • Where, exactly are you stuck? You've shown what your desired output is, but not your current output. Commented Jul 28, 2017 at 19:32

1 Answer 1

1

This may be a way to parse the data into your DataSet object, which you could then use to output in some other format like xml or json. There may be easier ways to do that though...

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));
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.