Question amout mtownsend/request-xml (XML to array) plugin - php

Have a question about mtownsend/request-xml (XML to array) plugin.
So, plugin makes XML file to array.
I use it in my Laravel projects and there is several reason, because I need exact it, but here is one problem.
Have two simple XML files
first file oneitem.xml with one item <flat> into <post>
<?xml version="1.0" encoding="UTF-8"?>
<data>
<post>
<flat>
<roms>4</roms>
<baths>2</baths>
</flat>
</post>
</data>
second file severalitems.xml one with several items <flat> into <post>:
<?xml version="1.0" encoding="UTF-8"?>
<data>
<post>
<flat>
<roms>4</roms>
<baths>2</baths>
</flat>
<flat>
<roms>5</roms>
<baths>1</baths>
</flat>
<flat>
<roms>7</roms>
<baths>3</baths>
</flat>
</post>
</data>
Then, I use a simple code to make an array from this files, and show the result array for each:
$xmlone = XmlToArray::convert(file_get_contents('public/xml/test/oneitem.xml'));
$oneflat = $xmlone['post'];
print_r($oneflat);
$xmlseveral = XmlToArray::convert(file_get_contents('public/xml/test/severalitems.xml'));
$severalflats = $xmlseveral['post'];
print_r($severalflats);
If we try to make an array from first file (with one flat), and find all we have in posts we have this result:
Array ( [flat] => Array ( [roms] => 4 [baths] => 2 ) )
If we do the same in second file (with several `flat), we have this result
Array ( [flat] => Array ( [0] => Array ( [roms] => 4 [baths] => 2 ) [1] => Array ( [roms] => 5 [baths] => 1 ) [2] => Array ( [roms] => 7 [baths] => 3 ) )
So, if we have several items, plugin adds a additional arrays with keys, [0], [1], [2]....
I need to do it the same, even there is just one item flat into posts. So the results have same formats.
I know, that it makes plugin. If plugin see, that there is just one flat in post, he makes result array simple.
The code of main file of plugin is here, but I cant understand, which lines do it...
Thanks for your help
public static function convert($xml, $outputRoot = false)
{
$array = self::xmlStringToArray($xml);
if (!$outputRoot && array_key_exists('#root', $array)) {
unset($array['#root']);
}
return $array;
}
protected static function xmlStringToArray($xmlstr)
{
$doc = new DOMDocument();
$doc->loadXML($xmlstr);
$root = $doc->documentElement;
$output = self::domNodeToArray($root);
$output['#root'] = $root->tagName;
return $output;
}
protected static function domNodeToArray($node)
{
$output = [];
switch ($node->nodeType) {
case XML_CDATA_SECTION_NODE:
case XML_TEXT_NODE:
$output = trim($node->textContent);
break;
case XML_ELEMENT_NODE:
for ($i = 0, $m = $node->childNodes->length; $i < $m; $i++) {
$child = $node->childNodes->item($i);
$v = self::domNodeToArray($child);
if (isset($child->tagName)) {
$t = $child->tagName;
if (!isset($output[$t])) {
$output[$t] = [];
}
$output[$t][] = $v;
} elseif ($v || $v === '0') {
$output = (string) $v;
}
}
if ($node->attributes->length && !is_array($output)) { // Has attributes but isn't an array
$output = ['#content' => $output]; // Change output into an array.
}
if (is_array($output)) {
if ($node->attributes->length) {
$a = [];
foreach ($node->attributes as $attrName => $attrNode) {
$a[$attrName] = (string) $attrNode->value;
}
$output['#attributes'] = $a;
}
foreach ($output as $t => $v) {
if (is_array($v) && count($v) == 1 && $t != '#attributes') {
$output[$t] = $v[0];
}
}
}
break;
}
return $output;
}
}
** Thanks for your help!**

Looks like its not posstible. As I understood, standart PHP tools to XML to array convertation makes it so. The plugin based on them.
Anyway, I think there is a way to solve it throught changing source code of this plugin, but I soleved in my situation, by workung with results of XML to array convertation, and check if result array have one flat or several.

Related

Array to xml - issue with repeated tags

EDITED
I'm trying to put my form inputs into an xml file.
Searching on this site I've found the following code and I used it to parse $_POST content.
After a few attempts I realized that "numeric tags" (resulting from not-associative arrays) could be reason of my insuccess so I modified the code as below:
function array_to_xml(array $arr, SimpleXMLElement $xml, $NumK = false)
{
foreach ($arr as $k => $v) {
if (is_array($v)){
preg_match('/^0|[1-9]\d*$/', implode(array_keys($v)))
? array_to_xml($v, $xml->addChild($k), true)
: array_to_xml($v, $xml->addChild($k));
}else{
$NumK
? $xml->addChild('_'.$k.'_', $v)
: $xml->addChild($k, $v);
}
}
return $xml;
}
Anyway I'm still "fighting" with xpath commands because I'm not able to find the GrandParent of some nodes (coming from not-associative arrays) that I need to convert into repeated tags.
That's the logic I'm trying to follow:
1st - Find nodes to reformat (The only ones having numeric tag);
2nd - Find grandparent (The tag I need to repeat);
3rd - Replace the grandparent (and his descendants) whith a grandparent's tag for each group of grandchilds (one for each child).
So far I'm still stuck on 1st step beacuse of xpath misunderstanding.
Below, the result xml I have and how I would to transform it:
My array is something like:
$TestArr = Array
("First" => array
("Martha" => "Text01"
,
"Lucy" => "Text02"
,
"Bob" => array
("Jhon" => array
("01", "02")
),
"Frank" => "One"
,
"Jessy" => "Two"
)
,
"Second" => array
("Mary" => array
("Jhon" => array
("03", "04")
,
"Frank" => array
("Three", "Four")
,
"Jessy" => array
("J3", "J4")
)
)
);
using the function array_to_xml($TestArr, new SimpleXMLElement('<root/>')) I get an xml like:
<root>
<First>
<Martha>Text01</Martha>
<Lucy>Text02</Lucy>
<Bob>
<Jhon>
<_0_>01</_0_>
<_1_>02</_1_>
</Jhon>
</Bob>
<Frank>One</Frank>
<Jessy>Two</Jessy>
</First>
<Second>
<Mary>
<Jhon>
<_0_>03</_0_>
<_1_>04</_1_>
</Jhon>
<Frank>
<_0_>Three</_0_>
<_1_>Four</_1_>
</Frank>
<Jessy>
<_0_>J3</_0_>
<_1_>J4</_1_>
</Jessy>
</Mary>
</Second>
</root>
My needed result is something like:
<root>
<First>
<Martha>Text01</Martha>
<Lucy>Text02</Lucy>
<Bob>
<Jhon>01</Jhon>
</Bob>
<Bob>
<Jhon>02</Jhon>
</Bob>
<Frank>One</Frank>
<Jessy>Two</Jessy>
</First>
<Second>
<Mary>
<Jhon>03</Jhon>
<Frank>Three</Frank>
<Jessy>J3</Jessy>
</Mary>
<Mary>
<Jhon>04</Jhon>
<Frank>Four</Frank>
<Jessy>J4</Jessy>
</Mary>
</Second>
</root>
I've updated the code to try and get closer to what you were trying to achieve. I've taken into the account of how to identify the grouping of data and to do this I've added an 'id' attribute to each of the elements added in this way. Also for convenience, I set a 'max' counter for the parent elements.
The first XPath expression (//*[#id]/..) fetches all the elements that needed to be processed. This then loops for the number of sub elements counted earlier. The XPath descendant::*[#id='{$i}'] picks out each set of elements (so all ones with id='0', then '1' etc.) This is the natural grouping of the data.
function array_to_xml(array $arr, SimpleXMLElement $xml, string $elementName = null)
{
foreach ($arr as $k => $v) {
if (is_array($v)){
if ( preg_match('/^0|[1-9]\d*$/', implode(array_keys($v)))) {
array_to_xml($v, $xml, $k);
}
else {
array_to_xml($v, $xml->addChild($k));
}
}
else {
if ( $elementName != null ) {
$newElement = $xml->addChild($elementName, $v);
$newElement["id"] = $k;
$xml["max"] = $k;
}
else {
$xml->addChild($k, $v);
}
}
}
//return $xml;
}
$xml = new SimpleXMLElement("<root />");
array_to_xml($TestArr, $xml);
$todoList = $xml->xpath("//*[#id]/..");
foreach ( $todoList as $todo ) {
$parent = $todo->xpath("..")[0];
for ( $i = 0; $i <= $todo['max']; $i++ ) {
$content = $todo->xpath("descendant::*[#id='{$i}']");
$newName = $todo->getName();
$new = $parent->addChild($newName);
foreach ( $content as $addIn ) {
$new->addChild($addIn->getName(), (string)$addIn);
}
}
unset ( $parent->$newName[0]);
}
print $xml->asXML();
Outputs...
<?xml version="1.0"?>
<root>
<First>
<Martha>Text01</Martha>
<Lucy>Text02</Lucy>
<Frank>One</Frank>
<Jessy>Two</Jessy>
<Bob>
<Jhon>01</Jhon>
</Bob>
<Bob>
<Jhon>02</Jhon>
</Bob>
</First>
<Second>
<Mary>
<Jhon>03</Jhon>
<Frank>Three</Frank>
<Jessy>J3</Jessy>
</Mary>
<Mary>
<Jhon>04</Jhon>
<Frank>Four</Frank>
<Jessy>J4</Jessy>
</Mary>
</Second>
</root>
Iterate your array value and add elements with the same key
if (is_array($v)){
foreach($v as $arr_ele){
$xml->addChild($k, $arr_ele);
}
}

Parse DOM to multidimensional array PHP

I want to Parse some DOM Content to a multidimensional array. Lets assume, I have this HTML Content
<for model="customer" id="0" processed="0">
<tag model="customer" value="name">name</tag>
<for model="accounts_receivable" processed="0">
<p>This is inside accounts_receivable</p>
</for>
</for>
I would like to parse this to:
array(
FOR => array (
ATTRIBUTES =>
SUBELEMENTS => array (
FOR => array (
ATTRIBUTES =>
SUBELEMENTS =>
)
)
)
)
I tried with parsing DOM via PHP with get ElementsByTagName, but its returning two for tags in the array.
The Key Point is, that the function should work with 2 layers or 20 layers.
Any good Idea?
Cheers,
Niklas
I wrote a function, doing this for "for" tag nodes. It ignores all other nodes, but parses recursively the complete DOM for all the for tag nodes.
$doc->loadHTML($this->template, LIBXML_NOWARNING | LIBXML_NOERROR);
$elements = $doc->getElementsByTagName('for');
$array = [];
if (!is_null($elements)) {
foreach ($elements as $element) {
if($element->getAttribute("processed") == false || $element->getAttribute("processed") != 1){
array_push($array, $this->parseDomChild($element));
}
}
}
function parseDomChild($element) {
$array = [];
if(isset($element->tagName) && $element->tagName == 'for') {
$array['nodeSelf'] = $element;
$element->setAttribute("processed", 1);
}
if($element->hasChildNodes()) {
$array['nodesChild'] = [];
foreach($element->childNodes as $node) {
array_push($array['nodesChild'], $this->parseDomChild($node));
}
}
return $array;
}

Convert PHP array from XML that contains duplicate elements

Up until now, I've been using the snippet below to convert an XML tree to an array:
$a = json_decode(json_encode((array) simplexml_load_string($xml)),1);
..however, I'm now working with an XML that has duplicate key values, so the array is breaking when it loops through the XML. For example:
<users>
<user>x</user>
<user>y</user>
<user>z</user>
</users>
Is there a better method to do this that allows for duplicate Keys, or perhaps a way to add an incremented value to each key when it spits out the array, like this:
$array = array(
users => array(
user_1 => x,
user_2 => y,
user_3 => z
)
)
I'm stumped, so any help would be very appreciated.
Here is a complete universal recursive solution.
This class will parse any XML under any structure, with or without tags, from the simplest to the most complex ones.
It retains all proper values and convert them (bool, txt or int), generates adequate array keys for all elements groups including tags, keep duplicates elements etc etc...
Please forgive the statics, it s part of a large XML tools set I used, before rewriting them all for HHVM or pthreads, I havent got time to properly construct this one, but it will work like a charm for straightforward PHP.
For tags, the declared value is '#attr' in this case but can be whatever your needs are.
$xml = "<body>
<users id='group 1'>
<user>x</user>
<user>y</user>
<user>z</user>
</users>
<users id='group 2'>
<user>x</user>
<user>y</user>
<user>z</user>
</users>
</body>";
$result = xml_utils::xml_to_array($xml);
result:
Array ( [users] => Array ( [0] => Array ( [user] => Array ( [0] => x [1] => y [2] => z ) [#attr] => Array ( [id] => group 1 ) ) [1] => Array ( [user] => Array ( [0] => x [1] => y [2] => z ) [#attr] => Array ( [id] => group 2 ) ) ) )
Class:
class xml_utils {
/*object to array mapper */
public static function objectToArray($object) {
if (!is_object($object) && !is_array($object)) {
return $object;
}
if (is_object($object)) {
$object = get_object_vars($object);
}
return array_map('objectToArray', $object);
}
/* xml DOM loader*/
public static function xml_to_array($xmlstr) {
$doc = new DOMDocument();
$doc->loadXML($xmlstr);
return xml_utils::dom_to_array($doc->documentElement);
}
/* recursive XMl to array parser */
public static function dom_to_array($node) {
$output = array();
switch ($node->nodeType) {
case XML_CDATA_SECTION_NODE:
case XML_TEXT_NODE:
$output = trim($node->textContent);
break;
case XML_ELEMENT_NODE:
for ($i = 0, $m = $node->childNodes->length; $i < $m; $i++) {
$child = $node->childNodes->item($i);
$v = xml_utils::dom_to_array($child);
if (isset($child->tagName)) {
$t = xml_utils::ConvertTypes($child->tagName);
if (!isset($output[$t])) {
$output[$t] = array();
}
$output[$t][] = $v;
} elseif ($v) {
$output = (string) $v;
}
}
if (is_array($output)) {
if ($node->attributes->length) {
$a = array();
foreach ($node->attributes as $attrName => $attrNode) {
$a[$attrName] = xml_utils::ConvertTypes($attrNode->value);
}
$output['#attr'] = $a;
}
foreach ($output as $t => $v) {
if (is_array($v) && count($v) == 1 && $t != '#attr') {
$output[$t] = $v[0];
}
}
}
break;
}
return $output;
}
/* elements converter */
public static function ConvertTypes($org) {
if (is_numeric($org)) {
$val = floatval($org);
} else {
if ($org === 'true') {
$val = true;
} else if ($org === 'false') {
$val = false;
} else {
if ($org === '') {
$val = null;
} else {
$val = $org;
}
}
}
return $val;
}
}
You can loop through each key in your result and if the value is an array (as it is for user that has 3 elements in your example) then you can add each individual value in that array to the parent array and unset the value:
foreach($a as $user_key => $user_values) {
if(!is_array($user_values))
continue; //not an array nothing to do
unset($a[$user_key]); //it's an array so remove it from parent array
$i = 1; //counter for new key
//add each value to the parent array with numbered keys
foreach($user_values as $user_value) {
$new_key = $user_key . '_' . $i++; //create new key i.e 'user_1'
$a[$new_key] = $user_value; //add it to the parent array
}
}
var_dump($a);
First of all this line of code contains a superfluous cast to array:
$a = json_decode(json_encode((array) simplexml_load_string($xml)),1);
^^^^^^^
When you JSON-encode a SimpleXMLElement (which is returned by simplexml_load_string when the parameter could be parsed as XML) this already behaves as-if there would have been an array cast. So it's better to remove it:
$sxml = simplexml_load_string($xml);
$array = json_decode(json_encode($sxml), 1);
Even the result is still the same, this now allows you to create a subtype of SimpleXMLElement implementing the JsonSerialize interface changing the array creation to your needs.
The overall method (as well as the default behaviour) is outlined in a blog-series of mine, on Stackoverflow I have left some more examples already as well:
PHP convert XML to JSON group when there is one child (Jun 2013)
Resolve namespaces with SimpleXML regardless of structure or namespace (Oct 2014)
XML to JSON conversion in PHP SimpleXML (Dec 2014)
Your case I think is similar to what has been asked in the first of those three links.

Search in hierarchical data in PHP

Here is the data structure I have (it's simplified for clearlier understanding):
• USA
• Alabama
• Montgomery
• Birmingham
• Arizona
• Phoenix
• Mesa
• Gilbert
• Germany
• West Germany
• Bonn
• Cologne
I need to return all path for given node – i.e.: if user enter Arizona, I need to return USA → Arizona. If enter Birmingham, I need to return USA → Alabama → Birmingham.
Is there in PHP simple way to search in such structures?
If you haven't huge data structure, you can use XML parsing. It's well known and easy to implement. It has desired ability to access parent element.
Here is an simple example:
$xml = <<<XML
<list>
<state name="USA">
<region name="Alabama">
<city name="Montgomery" />
<city name="Birmingham" />
</region>
<region name="Arizona">
<city name="Phoenix" />
<city name="Mesa" />
<city name="Gilbert" />
</region>
</state>
<state name="Germany">
<region name="West Germany">
<city name="Bonn" />
<city name="Cologne" />
</region>
</state>
</list>
XML;
$doc = new \DOMDocument;
$doc->preserveWhiteSpace = false;
$doc->loadXML($xml);
$xpath = new \DOMXPath($doc);
// XPath query to match all elements with
// attribute name equals to your searched phrase
$locations = $xpath->query("//*[#name='Cologne']");
function parse($list) {
$response = [];
foreach ($list as $node) {
$response[] = $node->attributes->getNamedItem('name')->nodeValue;
$parentNode = $node->parentNode;
// traverse up to root element
// root element has no attributes
// feel free to use any other condition, such as checking to element's name
while ($parentNode->hasAttributes()) {
$response[] = $parentNode->attributes->getNamedItem('name')->nodeValue;
$parentNode = $parentNode->parentNode;
}
}
return $response;
}
$parsedLocations = array_reverse(parse($locations));
echo implode(' → ', $parsedLocations), PHP_EOL;
Here is a possible strategy that builds up the path piece by piece: you start from the first level of the array and you check whether the searc term equals the key. If not, you check the value and otherwise if the value is an array (is_array()) you repeat the search recursively by using a prefix.
The data set:
$str = array(
"USA" => array(
"Alabama" => array(
"Montgomery",
"Birmingham"
),
"Arizona" => array(
"Phoenix",
"",
"Gilbert"
),
"West Germany" => array(
"Bonn",
"",
"Cologne"
)
),
"Germany" => array(
"West Germany" => array(
"Bonn",
"Mesa",
"Cologne"
)
)
);
The function:
function getPath($haystack, $needle, $prefix=""){
$path = "";
foreach($haystack as $key=>$value){
if($path!="")break;
if($key===$needle){
return $prefix.$key;
break;
}
elseif($value===$needle) {
return $prefix.$value;
break;
}
elseif(is_array($value)) {
$path.=getPath($value,$needle,$prefix.$key."=>");
}
}
return $path;
}
a test:
echo getPath($str,"Mesa");
In case of duplicates you will get the first result. If the search term is not found, you get an empty string.
Since "data structure" is very vague, and your only hint is that you're using PHP, I will assume that your "data structure" means the following:
[
'USA' =>
[
'Alabama' =>
[
'Montgomery',
'Birmingham'
],
'Arizona' =>
[
'Phoenix',
'Mesa',
'Gilbert'
]
],
'Germany' =>
[
'West Germany' =>
[
'Bonn',
'Cologne'
]
]
]
And I assume that you want your result in the form
['USA', 'Alabama', 'Birmingham']
If this is not the case, please inform us about how your data is actually available and how you want your result.
Is there in PHP simple way to search in such structures?
That depends on your definition of "simple".
For me, a solution that fits into a single function is "simple".
However, there is no out-of-the-box solution for this that you can use in a one-liner.
If you only need to find the "leafs", you could use a RecursiveIteratorIterator over a RecursiveArrayIterator as in this StackOverflow question.
But since you need to find intermediary keys too, that it not really an option.
The same goes for array_walk_recursive.
You could probably use ArrayIterator or array_walk, but in this example they can't really do anything a foreach loop can't, besides complicate things.
So I'd just go with a foreach loop:
function findMyThing($needle, $haystack) // Keep argument order from PHP array functions
{
// We need to set up a stack array + a while loop to avoid recursive functions for those are evil.
// Recursive functions would also complicate things further in regard of returning.
$stack =
[
[
'prefix' => [],
'value' => $haystack
]
];
// As long as there's still something there, don't stop
while(count($stack) > 0)
{
// Copy the current stack and create a new, empty one
$currentStack = $stack;
$stack = [];
// Work the stack
for($i = 0; $i < count($currentStack); $i++)
{
// Iterate over the actual array
foreach($currentStack[$i]['value'] as $key => $value)
{
// If the value is an array, then
// 1. the key is a string (so we need to match against it)
// 2. we might have to go deeper
if(is_array($value))
{
// We need to build the current prefix list regardless of what we're gonna do below
$prefix = $currentStack[$i]['prefix'];
$prefix[] = $key;
// If the current key, is the one we're looking for, heureka!
if($key == $needle)
{
return $prefix;
}
// Otherwise, push prefix & value onto the stack for the next loop to pick up
else
{
$stack[] =
[
'prefix' => $prefix,
'value' => $value
];
}
}
// If the value is NOT an array, then
// 1. the key is an integer, so we DO NOT want to match against it
// 2. we need to match against the value itself
elseif($value == $needle)
{
// This time append $value, not $key
$prefix = $currentStack[$i]['prefix'];
$prefix[] = $value;
return $prefix;
}
}
}
}
// At this point we searched the entire array and didn't find anything, so we return an empty array
return [];
}
Then just use it like
$path = findMyThing('Alabama', $array);
#Siguza
avoid recursive functions for those are evil
Recursion is not evil (or eval) and works well with stacks to
function df($v,array &$in,array &$stack,$search) {
$stack[] = $v;
if ( $v == $search ) {
return [true,$stack];
}
if ( is_array($in) ) {
foreach ($in as $vv => $k) {
if ( is_array($k) ) {
$r = df($vv, $k, $stack, $search);
if ($r[0]) {
return $r;
}
}
else if ($k == $search) {
$stack[] = $k;
return [true,$stack];
}
}
}
array_pop($stack);
return [false,null];
}
Usage:
$s = [];
$r = df('',$in,$s,'Bonn');
print_r($r);
$s = [];
$r = df('',$in,$s,'West Germany');
print_r($r);
$s = [];
$r = df('',$in,$s,'NtFound');
print_r($r);
Output:
Array
(
[0] => 1
[1] => Array
(
[0] =>
[1] => Germany
[2] => West Germany
[3] => Bonn
)
)
Array
(
[0] => 1
[1] => Array
(
[0] =>
[1] => Germany
[2] => West Germany
)
)
Array
(
[0] =>
[1] =>
)
According to you data structure.
$data['USA'] = ['Alabama' => ['Montgomery','Birmingham'],'Arizona' => ['Phoenix','Mesa','Gilbert']];
$data['Germany'] = ['West Germany' => ['Bonn','Cologne']];
function getHierarchy($location, $data){
$totalCountries = count($data);
//Get Array Keys of rows eg countries.
$keys = array_keys($data);
$hierarchy= [];
//Loop Through Countries
for($i = 0; $i < $totalCountries; $i++){
//If we have found the country then return it.
if($location == $keys[$i]) return [$keys[$i]];
$hierarchy[] = $keys[$i];
foreach($data[$keys[$i]] as $city => $places){
// if we have found the city then return it with country.
if($city == $location){
$hierarchy[] = $city;
return $hierarchy;
}
// if we have found the place in our places array then return it with country -> city -> place.
if(in_array($location, $places)){
$hierarchy[] = $city;
$hierarchy[] = $location;
return $hierarchy;
}
}
// Reset Hirarcy if we do not found our location in previous country.
$hierarchy = [];
}
}
$found = getHierarchy('Birmingham', $data);
if($found){
echo implode(' -> ', $found);
// Output will be USA -> Alabama -> Birmingham
}
It can only find only one country city and places and if any location is found then it will break the whole function and return the first location with city and place.
Here is more improved version which can find multiple locations as well.
https://gist.github.com/touqeershafi/bf89351f3b226aae1a29
Hope it helps you.

PHP parse assoc. array or XML

How to acces this assoc array?
Array
(
[order-id] => Array
(
[0] => 1
[1] => 2
)
)
as a result of XML parsing
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE request SYSTEM "http://shits.com/wtf.dtd">
<request version="0.5">
<order-states-request>
<order-ids>
<order-id>1</order-id>
<order-id>2</order-id>
...
</order-ids>
</order-states-request>
</request>
$body = file_get_contents('php://input');
$xml = simplexml_load_string($body);
$src = $xml->{'order-states-request'}->{'order-ids'};
foreach ($src as $order) {
echo ' ID:'.$order->{'order-id'};
// dont work - echoes only ID:1, why?
}
// ok, lets try just another way...
$items = toArray($src); //googled function - see at the bottom
print_r($items);
// print result - see at the top assoc array
// and how to acces order ids in this (fck) assoc array???
//------------------------------------------
function toArray(SimpleXMLElement $xml) {
$array = (array)$xml;
foreach ( array_slice($array, 0) as $key => $value ) {
if ( $value instanceof SimpleXMLElement ) {
$array[$key] = empty($value) ? NULL : toArray($value);
}
}
return $array;
}
MANY THANKS FOR ANY HELP!
What you want is:
$body = file_get_contents('php://input');
$xml = simplexml_load_string($body);
$src = $xml->{'order-states-request'}->{'order-ids'}->{'order-id'};
foreach ($src as $id)
{
echo ' ID:', $id, "\n";
}
Live DEMO.
What happens with your code is that you're trying to loop:
$xml->{'order-states-request'}->{'order-ids'}
Which is not the array you want, order-id is, as you can see on your dump:
Array
(
[order-id] => Array

Categories