Simplexmlelement to PHP Array

SimpleXMLElement to PHP Array

The $answer can already work as an array. You can do this if you want put it in a real array,

$array = array();
foreach($answer as $k => $v) {
$array[$k] = $v;
}

Converting a SimpleXML Object to an Array

I found this in the PHP manual comments:

/**
* function xml2array
*
* This function is part of the PHP manual.
*
* The PHP manual text and comments are covered by the Creative Commons
* Attribution 3.0 License, copyright (c) the PHP Documentation Group
*
* @author k dot antczak at livedata dot pl
* @date 2011-04-22 06:08 UTC
* @link http://www.php.net/manual/en/ref.simplexml.php#103617
* @license http://www.php.net/license/index.php#doc-lic
* @license http://creativecommons.org/licenses/by/3.0/
* @license CC-BY-3.0 <http://spdx.org/licenses/CC-BY-3.0>
*/
function xml2array ( $xmlObject, $out = array () )
{
foreach ( (array) $xmlObject as $index => $node )
$out[$index] = ( is_object ( $node ) ) ? xml2array ( $node ) : $node;

return $out;
}

It could help you. However, if you convert XML to an array you will loose all attributes that might be present, so you cannot go back to XML and get the same XML.

How to convert array to SimpleXML

a short one:

<?php

$test_array = array (
'bla' => 'blub',
'foo' => 'bar',
'another_array' => array (
'stack' => 'overflow',
),
);
$xml = new SimpleXMLElement('<root/>');
array_walk_recursive($test_array, array ($xml, 'addChild'));
print $xml->asXML();

results in

<?xml version="1.0"?>
<root>
<blub>bla</blub>
<bar>foo</bar>
<overflow>stack</overflow>
</root>

keys and values are swapped - you could fix that with array_flip() before the array_walk. array_walk_recursive requires PHP 5. you could use array_walk instead, but you won't get 'stack' => 'overflow' in the xml then.

PHP get values from SimpleXMLElement array

With SimpleXML, you can get :

  • sub-elements, using object notation : $element->subElement
  • and attributes, using array notation : $element['attribute']


So, here, I'd say you'd have to use :

echo $child['name'];


As a reference, and for a couple of examples, see the Basic usage section of simplexml's manual.

Example #6 should be the interesting one, about attributes.

SimpleXML Attributes to Array

Don't directly read the '@attributes' property, that's for internal use. Anyway, attributes() can already be used as an array without needing to "convert" to a real array.

For example:

<?php
$xml = '<xml><test><a a="b" r="x" q="v" /></test><b/></xml>';
$x = new SimpleXMLElement($xml);

$attr = $x->test[0]->a[0]->attributes();
echo $attr['a']; // "b"

If you want it to be a "true" array, you're gonna have to loop:

$attrArray = array();
$attr = $x->test[0]->a[0]->attributes();

foreach($attr as $key=>$val){
$attrArray[(string)$key] = (string)$val;
}

Check if it is an array from SimpleXMLElement object

Consider a simplified example:

<root>
<element>first</element>
<element>second</element>
<element>third</element>
</root>

$root->element is not really an array. It is SimpleXMLElement object. You can think of it as a selector, a collection of elements you can traverse using foreach and also access specific object using indexes:

$root->element[0]; //first object
$root->element[1]; //second
$root->element[2]; //third

Here is more examples of basic usage: http://php.net/manual/en/simplexml.examples-basic.php

So the real question is: How to define if there is more than one element in collection ?

You can do it using count() method:

if($es->result->hotel->channel->room_price->price->count() > 1){
echo 'many elements';
}

Read more: http://php.net/manual/en/simplexmlelement.count.php

Get values from SimpleXMLElement Object for php array

You are using the same variable name for two different things:

foreach($xml->item as $rate){
// at this point, $rate is the <item> element

$rate = (string) $rate->exchangeRate;
// now $rate is a string with the exchange rate

$curr_code = (string) $rate->targetCurrency;
// so now this won't work

$money[] = array('rate' => $rate, 'curr_code' => $curr_code);
}

If you were running with display_errors switched on or checking your logs, you would have seen a message like this:

Notice: Trying to get property 'targetCurrency' of non-object

Or in PHP 8, this:

Warning: Attempt to read property "targetCurrency" on string


The fix is simply to name your variables more carefully:

foreach($xml->item as $itemElement){
$rate = (string) $itemElement->exchangeRate;
$curr_code = (string) $itemElement->targetCurrency;
$money[] = array('rate' => $rate, 'curr_code' => $curr_code);
}


Related Topics



Leave a reply



Submit