There is XML received from a third-party server:

 object(SimpleXMLElement)#4 (1) { ["@attributes"]=> array(5) { ["message_id"]=> string(1) "0" ["message_phone"]=> string(11) "**********" ["message_parts"]=> string(1) "1" ["message_zone"]=> string(1) "1" ["message_cost"]=> string(3) "0,6" } } 

Either I'm stupid, or I just don't understand, I need to pull out the @attributes values

 $xml=simplexml_load_string($str); 

But how do I get to $xml->@attributes ? Sign @ not put - you can not.

    2 answers 2

    You can use get_object_vars :

     $xml=simplexml_load_string($str); $a = get_object_vars($xml); var_dump($a["@attributes"]); 

    UPDATE

    In the case of PHP 5.3, it is possible without crutches:

     $xml = simplexml_load_string($str); $a = (array)$xml; var_dump($a["@attributes"]); 
    • Well done ATP +1 you. - Artem
    • There is just another illogic in php, from an array to an object is very simple: $obj = (object)$array; , but back ... $arr = (array)$obj , although strangely, it works on 5.3, there were problems before - chernomyrdin
    • This is it! But I don’t need to be pushed back, I’m just going to parse the INSERT in the database and already there is a script case. - Artem

    Alternatively, work as with an array: $ xml ['message_cost'] returns "0.6".

    • I put here only a part of the XML array, it is huge :) it's kind of like 5th nesting into it :) i.e. There are sections in XML subsections, etc. and only there are parameters to the sections, that's how it is bent :) - Artem
    • Not a question: I once sorted html in a similar way. There something turned out of type $table->tbody->tr[$i]->td[$j]->input[0]['value'] . =) - ling
    • All answers are taken into account;) in, there was such a topic about a year and a half ago. - Artem