is there an object function for type array_reverse There is such an object

  object(SimpleXMLElement)[2666] public '@attributes' => array (size=2) 'id' => string '3451' (length=4) 'available' => string 'true' (length=4) public 'url' => string 'http://miliydom.com.ua//single.php?id=3451' (length=42) public 'vendorCode' => string 'β„–0050-55' (length=10) public 'price' => string '780' (length=3) public 'delivery' => string 'true' (length=4) public 'currencyId' => string 'UAH' (length=3) public 'country_of_origin' => string 'Π£ΠΊΡ€Π°ΠΈΠ½Π°' (length=14) public 'categoryId' => string '53' (length=2) public 'name' => string 'ΠŸΠ°Ρ€ΠΊΠ° "Π—Π½Π°Ρ‡ΠΊΠΈ"' (length=25) object(SimpleXMLElement)[112] public '@attributes' => array (size=2) 'id' => string '3450' (length=4) 'available' => string 'true' (length=4) public 'url' => string 'http://miliydom.com.ua//single.php?id=3450' (length=42) public 'vendorCode' => string 'β„–0050-50' (length=10) public 'price' => string '780' (length=3) public 'delivery' => string 'true' (length=4) public 'currencyId' => string 'UAH' (length=3) public 'country_of_origin' => string 'Π£ΠΊΡ€Π°ΠΈΠ½Π°' (length=14) public 'categoryId' => string '53' (length=2) public 'name' => string 'ΠŸΠ°Ρ€ΠΊΠ° "ВСлосипСдики"' (length=37) 

me through the foreach to sort them in reverse order how is this possible?

  • And how do you get them in the "direct" order? - rjhdby
  • and the for loop has already been canceled, right? - Alexey Shimansky
  • one
    As a crutch, I can suggest iterating in the direct order, when iterating, write the object into an array, and then get the array in reverse order. I wonder what problem this may be needed for? - heff
  • @ Alexey Shimansky, and how to do this in for? I am so on the foreach shot wrote - Sergalas
  • @rjhdby in an xml document that is not mine I’m just using it trying to write data into the database - Sergalas

2 answers 2

If these objects get to you AS IS, and you need, for some reason, to get a list of its public properties, then it makes sense to use Reflection .

 class Foo{ public $a=10; public $b="test"; }; $foo = new Foo(); $reflect = new ReflectionClass($foo); $props = $reflect->getProperties(ReflectionProperty::IS_PUBLIC); foreach ($props as $prop) { $name = $prop->getName(); $array[$name] = $foo->$name; } 

And then use this array in any order.

    The object simplexml is quite adequately translated into a regular array by regular methods:

     $xml = new SimpleXmlElement('<?xml version="1.0" encoding="UTF-8" ?><foo bar="1"><x>3</x><a>1</a><b>2</b></foo>'); $arr = (array) $xml; ksort($arr); var_dump($xml, $arr); 

    Instead of casting to an array, you can use the get_object_vars function, the effect is generally the same.

    A separate question is why did you ever need to somehow display the original XML. Usually the developer knows what fields in this XML are and displays the necessary treatment in the right places in the normal manner by the property name.

    • xml is not mine, I just try to write it into the database on its basis - Sergalas
    • "The object simplexml is adequately translated into a regular array by regular methods" But what about the opp :) - Sergalas
    • Then what for you need some sort of order? Refer to the specific required fields and form your record in the database. And if one element in XML gets you with a different order of fields? Will the whole process crash or will you write something wrong in the database? And aop for aop does not make sense. - Shallow