Jump to content

hover

New Members
  • Posts

    1
  • Joined

  • Last visited

hover's Achievements

Newbie

Newbie (1/5)

0

Reputation

  1. 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.
×
×
  • 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.