开发者

Trouble Storing SimpleXMLElement Object into Array

开发者 https://www.devze.com 2023-03-13 18:31 出处:网络
Array ( [0] => SimpleXMLElement Object ( [record] => SimpleXMLElement Object ( [f] => Array ( [0] => Company
Array ( [0] => 
   SimpleXMLElement Object ( [record] => 
   SimpleXMLElement Object ( [f] => 
                             Array ( [0] => Company 
                                     [1] => Company 
                                     [2] => Mark Test 
                                     [3] => Intern 
                                     [4] => Administrative 
                                     [5] => Account 
                                     [6] => img.png 
                                     [7] => SimpleXMLElement Object ( [@attributes] => Array ( [id] => 18 ) ) 
                                     [8] => SimpleXMLElement Object ( [@attributes] => Array ( [id] => 21 ) ) 
    开发者_JS百科                                 [9] => emailtest@gmail.com 
                                     [10] => SimpleXMLElement Object ( [@attributes] => Array ( [id] => 178 ) ) 
                                     [11] => SimpleXMLElement Object ( [@attributes] => Array ( [id] => 177 ) ) 
                                     [12] => SimpleXMLElement Object ( [@attributes] => Array ( [id] => 179 ) ) 
                                     [13] => SimpleXMLElement Object ( [@attributes] => Array ( [id] => 168 ) ) ) 
                            [update_id] => 12102222262043 ) ) )

This is basically my simplexmlobject array that I want to somehow just manipulate and take out the data starting from the f element. I want to store like the words Company, Mark Test, Intern into an array. I can't seem to figure out how I am suppose to do this with a foreach loop in php and the structure of this is so crazy.

Well to get all the values for this data I did this in part of my code where getUsers() does an api call to a service that gives me a simplexml:

$xml = $getUser->getUsers();
$records = $xml->xpath('//records');

print_r($records);


The DOMDocument class behaves a little better than SimpleXML in cases like this (when you want to do something slightly more complicated)...

First load the document:

$dom = new DOMDocument();
$dom->load($filename);

Then get all the <records> nodes:

// Get a list of all <records> nodes which you can then loop through with foreach
$records = $dom->getElementsByTagName('records');

To get the children of all the <records> nodes:

$allChildren = array();
foreach ( $records as $recordSet ) {
   foreach( $recordSet->childNodes as $child ) {
     $allChildren[] = $child;
   }
}

Now lets say you want the value of <first_name> in the first <records> node:

$value = $records->item(0)->getElementsByTagName('first_name')->item(0)->nodeValue;

Note that the return values from getElementsByTagName are DOMNodeLists not arrays.

As an added bonus if you know Javascript, this class behaves very much like the DOM is Javascript.

See also: http://www.php.net/manual/en/book.dom.php

0

精彩评论

暂无评论...
验证码 换一张
取 消