I am working with WordPress and since I don’t believe it is possible to sort object details, I was wondering how to go about converting my Object to an Array, so that sorting can be possible.
Any help or guidance would be greatly appreciated.
I am using the WP function get_categories();
The complete content of $category is:
$category->term_id
$category->name
$category->slug
$category->term_group
$category->term_taxonomy_id
$category->taxonomy
$category->description
$category->parent
$category->count
$category->cat_ID
$category->category_count
$category->category_description
$category->cat_name
$category->category_nicename
$category->category_parent
If the object is not too complex (in terms of nesting) you can cast the class to an array:
outputs:
However this will only convert the base level. If you have nested objects such as
Then only the base object will be cast to an array.
In order to go deeper, you would have to use recursion. There is a good example of an object to array conversion here.