I have an object structured like so:
Array
(
[0] => SimpleXMLElement Object ( [0] => Europe )
[1] => SimpleXMLElement Object ( [0] => South America )
[3] => SimpleXMLElement Object ( [0] => North America )
[4] => SimpleXMLElement Object ( [0] => Asia )
)
I am trying to sort the objects alphabetically. I’ve tried using sort() on the array as a whole, but it’s not working. I am assuming it was just grabbing the name of the object, which is the same in all cases and sorting those instead. I am trying to access the text inside each object but can’t seem to do it without bringing the ‘SimpleXMLElement Object()’ text with it. How would I access that text and perhaps recreate a new array with just the text values?
EDIT: I’ve tried the following:
$regions = sort($regions);
$regions = usort($regions);
Thanks!
You can use usort:
This will keep the data structure of your array. But if you don’t care about your SimpleXML elements, just export it in a clean and more memory efficient array.
Note the (string) cast, allowing to get the actual value of the simpleXmlElement.