Jump to content

From one array, to another array


hover

Recommended Posts

Hi!
 
Im trying to fill $array with data from another array, $parent_array
 
I would like $array to look like:
 

stdClass Object
(
[1] => stdClass Object
(
[titel] => Hero1
[info] =>
[href] => http://example.com/1
)

[2] => stdClass Object
(
[titel] => Hero2
[info] => This is a test
[href] =>
)
) and so on.....

$parent_array is a DOMElement Obejct and a DOMAttr Object and look like:
 

Array
(
[0] => DOMElement Object
(
[tagName] => span
[schemaTypeInfo] =>
[nodeName] => span
[nodeValue] => Hero1
[nodeType] => 1
[parentNode] => (object value omitted)
[childNodes] => (object value omitted)
[firstChild] => (object value omitted)
[lastChild] => (object value omitted)
[previousSibling] => (object value omitted)
[nextSibling] => (object value omitted)
[attributes] => (object value omitted)
[ownerDocument] => (object value omitted)
[namespaceURI] =>
[prefix] =>
[localName] => span
[baseURI] =>
[textContent] => Hero1
)

[1] => DOMElement Object
(
[tagName] => div
[schemaTypeInfo] =>
[nodeName] => div
[nodeValue] => 
[nodeType] => 1
[parentNode] => (object value omitted)
[childNodes] => (object value omitted)
[firstChild] => (object value omitted)
[lastChild] => (object value omitted)
[previousSibling] => (object value omitted)
[nextSibling] => (object value omitted)
[attributes] => (object value omitted)
[ownerDocument] => (object value omitted)
[namespaceURI] =>
[prefix] =>
[localName] => div
[baseURI] =>
[textContent] => 
)
[2] => DOMAttr Object

(
[name] => href
[specified] => 1
[value] => http://example.com/1
[ownerElement] => (object value omitted)
[schemaTypeInfo] =>
[nodeName] => href
[nodeValue] => http://example.com/1
[nodeType] => 2
[parentNode] => (object value omitted)
[childNodes] => (object value omitted)
[firstChild] => (object value omitted)
[lastChild] => (object value omitted)
[previousSibling] => (object value omitted)
[nextSibling] => (object value omitted)
[attributes] =>
[ownerDocument] => (object value omitted)
[namespaceURI] =>
[prefix] =>
[localName] => href
[baseURI] =>
[textContent] => http://example.com/1
)

Currently i can achieve the following:
 

stdClass Object
(
[0] => stdClass Object
(
[titel] => Hero1
)

[1] => stdClass Object
(
[info] => 
)

[2] => stdClass Object
(
[href] => http://example.com/1
)
 
[3] => stdClass Object
(
[titel] => Hero2
)

[4] => stdClass Object
(
[info] => This is a test
)

[5] => stdClass Object
(
[href] =>
)

 
with this code:

 

    $array = (object) array();
            foreach ($parent_array as $key => $node) {
                        $array->{$key} = (object) array();
                if (get_class($node) === 'DOMElement') {
                    if ($node->hasAttribute('class')) {
                        if ($node->getAttribute('class') === 'titel') {
                                $array->{$key}->salong = $node->nodeValue;
                        }elseif($node->getAttribute('class') === 'info'){
                                $array->{$key}->time = $node->nodeValue;
                        }
                    }
                }
                if (get_class($node) === 'DOMAttr') {
                    if ($node->nodeName === 'href') {
                            $array->{$key}->href = $node->nodeValue;
                    }
                }
                
            }

 

But as you can see, it is not multi dimensional as intended. I am not irerating over $parent_array into $array the right way.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.