Muhammad Bilal
Muhammad Bilal

Reputation: 57

How to store data in Object to an array in php

againAnotherChild is an object now how can i put the data from it to an array.

foreach($anotherChild->children() as $againAnotherChild) //child to 
//childchildchild
    {
        // echo "Inside Again child Tag attributes<br>";
        $againAnotherChildArray[] = $againAnotherChild->getName();
        //print_r($againAnotherChild);
//            foreach($this->$againAnotherChild[0] as $Storage)
//            {
//                $store = $Storage;
//                //echo $store;
//            }
        echo $againAnotherChild[0]."<br>";
        //echo "Storage".$store;
    }

if i do print_r($againAnotherChild) this is what i get which updates after each iteration

SimpleXMLElement Object ( [0] => uint8 ) SimpleXMLElement Object ( [0] => uint8 ) SimpleXMLElement Object ( [0] => enum ) SimpleXMLElement Object ( [0] => uint8 ) SimpleXMLElement Object ( [0] => enum ) SimpleXMLElement Object ( [0] => uint8 ) SimpleXMLElement Object ( [0] => firmware ) SimpleXMLElement Object ( [0] => enum ) SimpleXMLElement Object ( [0] => enum ) SimpleXMLElement Object ( [0] => uint8 ) SimpleXMLElement Object ( [0] => enum )

------------------------Next Iteration--------------

SimpleXMLElement Object ( [0] => nodeid ) SimpleXMLElement Object ( [0] => uint8 ) SimpleXMLElement Object ( [0] => ipaddress ) SimpleXMLElement Object ( [0] => macaddress ) SimpleXMLElement Object ( [0] => enum ) SimpleXMLElement Object ( [0] => uint8 ) SimpleXMLElement Object ( [0] => uint16 ) SimpleXMLElement Object ( [0] => uint16 ) SimpleXMLElement Object ( [0] => uint16 ) SimpleXMLElement Object ( [0] => uint16 ) SimpleXMLElement Object ( [0] => enum ) SimpleXMLElement Object ( [0] => enum ) SimpleXMLElement Object ( [0] => uint16 ) SimpleXMLElement Object ( [0] => uint16 ) SimpleXMLElement Object ( [0] => uint16 ) SimpleXMLElement Object ( [0] => uint16 ) SimpleXMLElement Object ( [0] => enum )

How can i put these uint8,uint16 etc into an array that keeps updating till last iteration?

Upvotes: 0

Views: 38

Answers (2)

Muhammad Bilal
Muhammad Bilal

Reputation: 57

The simple solution was just to type cast an object to certain data type

    $store = (array)againAnotherChildArray;

What i did in my code is this

                    $nodesWithValues = (array)$anotherChild->children();
                    foreach ($nodesWithValues as $key => $value)
                    {
                        //echo "$key : $value <br>";//CfgVer : uint8
                        var_dump($nodesWithValues);
                    }

problem solved by simple type casting :D

Upvotes: 1

Dolfa
Dolfa

Reputation: 792

Are you asking about removing duplicates in array?

Would this help?

$againAnotherChildArray[$againAnotherChild->getName()] = $againAnotherChild->getName();

That should create something like hash map, eg set of tuples;

Upvotes: 0

Related Questions