php sort properties of object
-
I want to sort the properties of an object so I can loop through them in a defined order. for example: I have an object 'book' with the following properties: 'id', 'title', 'author', 'date'. Now i want to loop through these properties like this: foreach($book as $prop=>$val) //do something now the order of the loop has to be 'title', then 'author', 'date' and 'id' How would one do this? (I can't change the order of the properties in the class of the object because there arent any properties defined there, I get the object from the database with 'MyActiveRecord')
-
Answer:
Not sure if this answers to your question, but you could try : $properties = array('title', 'author', 'date', 'id'); foreach ($properties as $prop) { echo $book->$prop; } Alternatively, you could extract the properties of the book (instead of hardcoding them), and sort them with your custom order : $props = get_class_vars(get_class($book)); uasort($props, 'my_properties_sorting_function');
user324107 at Stack Overflow Visit the source
Other answers
You could wrap your source object into something that implements the http://docs.php.net/class.iterator and returns the properties of the source object in a given sort order. class Foo implements Iterator { protected $src; protected $order; protected $valid; public function __construct(/*object*/ $source, array $sortorder) { $this->src = $source; $this->order = $sortorder; $this->valid = !empty($this->order); } public function current() { return $this->src->{current($this->order)}; } public function key() { return key($this->order); } public function next() { $this->valid = false!==next($this->order); } public function rewind() { reset($this->order); $this->valid = !empty($this->order); } public function valid() { return $this->valid; } } class Book { public $id='idval'; public $author='authorval'; public $date='dateval'; public $title='titleval'; } function doSomething($it) { foreach($it as $p) { echo ' ', $p, "\n"; } } $book = new Book; echo "passing \$book to doSomething: \n"; doSomething($book); $it = new Foo($book, array('title', 'author', 'date', 'id')); echo "passing \$it to doSomething: \n"; doSomething($it); prints passing $book to doSomething: idval authorval dateval titleval passing $it to doSomething: titleval authorval dateval idval
VolkerK
Try this. The difference between this method and the method proposed in the first response is that the method get_class_vars returns default properties of the class, whereas get_object_vars returns the properties of the given object. <?php class Book { public $title; public $author; public $date; public $id; } $b = new Book(); $b->title = "title"; $object_vars = get_object_vars($b); ksort($object_vars); foreach($object_vars as $prop=>$val) echo $prop."->".$val."<br>"; ?>
Aleh
Related Q & A:
- How can I retrieve the object properties values from the database?Best solution by Stack Overflow
- Object reference not set to an instance of an object; null?Best solution by Stack Overflow
- How to dynamically create object properties from an array in Javascript?Best solution by nfriedly.com
- How to sort collection of object?Best solution by Stack Overflow
- How to create an Object with multiple properties of the same name?Best solution by javascript.info
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.