I have a DOMNodeList array that i am trying to convert into a multidimensional array. This is my attempt to do that:
$array = (object) array();
foreach ($this->timeofday as $key => $node) {
$array->{$key} = (object) array();
if (get_class($node) === 'DOMElement') {
if ($node->hasAttribute('class')) {
if ($node->getAttribute('class') === 'cmil_salong') {
$array->{$key}->salong = $node->nodeValue;
}
if($node->getAttribute('class') === 'cmil_time'){
$array->{$key}->time = $node->nodeValue;
}
if($node->getAttribute('class') === 'mv_3d'){
$array->{$key}->threed = $node->nodeValue;
}
if($node->getAttribute('class') === 'cmil_rs'){
$array->{$key}->freeseats = $node->nodeValue;
}
if($node->getAttribute('class') === 'mv_txt'){
$array->{$key}->textad = $node->nodeValue;
}
}
}
if (get_class($node) === 'DOMAttr') {
if ($node->nodeName === 'href') {
$array->{$key}->href = $node->nodeValue;
}
}
}
That produces:
stdClass Object
(
[0] => stdClass Object
(
[salong] => Tokyo
)
[1] => stdClass Object
(
[time] => 15:00
)
[2] => stdClass Object
(
[threed] =>
)
[3] => stdClass Object
(
[freeseats] => 20
)
[4] => stdClass Object
(
[textad] =>
)
[5] => stdClass Object
(
[href] => http://example.com/1
)
And so on..
But the expected result is:
stdClass Object
(
[1] => stdClass Object
(
[salong] => Tokyo
[time] => 11:30
[threed] =>
[textad] => 1
[freeseats] => 20
[href] => http://example.com/1
)
[2] => stdClass Object
(
[salong] ... And so on
)
may be this is the thing you want :
$mainKey=-1;$keyCount=0;
$array = (object) array(); $parant = "";
foreach ($this->timeofday as $key => $node) {
if(($keyCount == 0) || $keyCount >= 5) {$keyCount = 0;
$mainKey++;$array->{$mainKey} = (object) array();}
$array->{$mainKey}->{$key} = (object) array();
if (get_class($node) === 'DOMElement') {
if ($node->hasAttribute('class')) {
if ($node->getAttribute('class') === 'cmil_salong') {
$array->{$mainKey}->{$key}->salong = $node->nodeValue;
}
if($node->getAttribute('class') === 'cmil_time'){
$array->{$mainKey}->{$key}->time = $node->nodeValue;
}
if($node->getAttribute('class') === 'mv_3d'){
$array->{$mainKey}->{$key}->threed = $node->nodeValue;
}
if($node->getAttribute('class') === 'cmil_rs'){
$array->{$mainKey}->{$key}->freeseats = $node->nodeValue;
}
if($node->getAttribute('class') === 'mv_txt'){
$array->{$mainKey}->{$key}->textad = $node->nodeValue;
}
}
}
if (get_class($node) === 'DOMAttr') {
if ($node->nodeName === 'href') {
$array->{$mainKey}->{$key}->href = $node->nodeValue;
}
}
}
There should be parent node checking or if nodes are 5 then count the nodes and change the main array index accordingly.