I have an object with 3 properties. I’d like to input a number 1,2, or 3 (0,1,or 2 is fine too) and sort the object in ascending numerical order based on one its property values.
Here’s what my object looks like:
var_dump($obj);
array(3) {
[0]=> object(stdClass)#25 (92) {
["file_id"]=> string(1) "6"
["name"]=> string(1) "1st item"
}
[1]=> object(stdClass)#26 (92) {
["file_id"]=> string(1) "7"
["name"]=> "2nd item"
}
[2]=> object(stdClass)#27 (92) {
["file_id"]=> string(1) "8"
["name"]=> "3rd item"
}
}
If I input 1, then the output would look like this:
file_id name
6 1st item
7 2nd item
8 3rd item
If I input 2, then the output would be:
7 2nd item
8 3rd item
6 1st item
If I input 3, then the output would be:
8 3rd item
6 1st item
7 2nd item
This question is nearly identical to one I asked earlier on Stackoverflow , the sole exception being that I need to sort() on the index positions of the file_id values and not on the file_id values themselves. I.e., I need to sort on 1,2,3 and not 6,7,8.
If you are particularly excited about this question (yes I realize this is unlikely), i’d be curious to know what the numbers 25 and 92 stand for in the output: object(stdClass)#25 (92).
As I understand your question after sorting an array by some property you want to rotate the array so that e.g. the array (1,2,3,4) becomes (3,4,1,2).
I’m using string literals as array members in this example, switching to objects is trivial.
prints