PHP parse assoc. array or XML -
how acces assoc array?
array ( [order-id] => array ( [0] => 1 [1] => 2 ) )
as 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 $order) { echo ' id:'.$order->{'order-id'};
// dont work - echoes id:1, why? }
// ok, lets try way...
$items = toarray($src); //googled function - see @ bottom print_r($items);
// print result - see @ top assoc array
// , how acces order ids in (fck) assoc array???
//------------------------------------------
function toarray(simplexmlelement $xml) { $array = (array)$xml; foreach ( array_slice($array, 0) $key => $value ) { if ( $value instanceof simplexmlelement ) { $array[$key] = empty($value) ? null : toarray($value); } } return $array; }
many help!
what want is:
$body = file_get_contents('php://input'); $xml = simplexml_load_string($body); $src = $xml->{'order-states-request'}->{'order-ids'}->{'order-id'}; foreach ($src $id) { echo ' id:', $id, "\n"; }
what happens code you're trying loop:
$xml->{'order-states-request'}->{'order-ids'}
which not array
want, order-id
is, can see on dump:
array ( [order-id] => array
Comments
Post a Comment