PHP解析关联。 数组或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, "
";
}

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