0

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).

4
  • Are you using a var_dump or a print_r to display the object info? Commented Aug 13, 2012 at 13:14
  • @Matt var_dump(). Is converting $obj to an array needed for indexing? I'm using Codeigniter query->result() which returns "an array of objects". Commented Aug 13, 2012 at 13:19
  • To answer your question about the numbers in the var_dump(), I think the #25 is the resource ID of the object; 92 may be the size of the object in bytes. Commented Aug 13, 2012 at 13:28
  • The second number (92) seems to be the number of properties of that object. I.e. class A { protected $p1; } -> (1), class B { protected $p1; protected $p2; } -> (2) and so on. This also applies to properties that are added only at runtime (i.e. outside the class contract). Commented Aug 14, 2012 at 6:58

3 Answers 3

2

I think you are looking for usort

Write 3 compare functions, for each attribute one, switch according to the input value, which compare function is used

edit: the numbers are the PHP internal object id (#25) and the size of the object.

quick example:

function compare_1($a, $b) {
  return strcmp($a->file_id, $b->file_id);
}
// compare_2, compare_3 accordingly as needed with your objects

switch ($input) {
  case 1:
    $compareFunctionName = 'compare_1';
    break;
  case 2:
    $compareFunctionName = 'compare_2';
    break;
  case 3:
    $compareFunctionName = 'compare_3';
    break;
  default:
    throw new Exception('wrong Parameter: input is ' . $input);
 }

 usort($objectArray, $compareFunctionName);

 var_dump($objectArray);
Sign up to request clarification or add additional context in comments.

3 Comments

-@cypherabe, thanks for taking a shot at this, there is an unknown # of objects so compare_1, compare_2,... isn't realistic. Any work arounds for that?
maybe I didn't understand your intention... my approach is that compare_1 etc should be functions that compare a specific attribute on all objects: compare_1 compares $obj->file_id, compare_2 compares $obj->name etc.. the number of objects in the array doesn't matter, as long as the objects are similar. You need to have some knowledge about the target objects to write this. - just saw VolkerK's answer...maybe i didn't catch the rotate part?
-@cypherabe, oh I understand what you did now. Its just that your code doesn't enable to input a value. I'm looking for something like @VolkerK's rotate() function where you can input the value $n.
1

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.

<?php
$sortedData = array('A', 'B', 'C', 'D', 'E'); // getting an array like this has been solved by the answers to your previous question
$foo = rotate($sortedData, 2);
var_dump($foo);


function rotate($source, $n) {
    // could use some pre-checks...
    return array_merge(
        array_slice($source, $n, NULL, true),
        array_slice($source, 0, $n, true)
    );
}

prints

array(5) {
  [0]=>
  string(1) "C"
  [1]=>
  string(1) "D"
  [2]=>
  string(1) "E"
  [3]=>
  string(1) "A"
  [4]=>
  string(1) "B"
}

1 Comment

-@VolkerK, thanks! nice work, your rotate() function did the trick!
0

Here is a simple algorithm to complete this task

Step1: remove the value of the inputted index from the array array_search() and unset function

Step2: Sort the array by using sort function

Step 3: Add the removed value to the top of the array by using push/pop function

For more knowledge on array functions visit http://www.phpsyntax.blogspot.com

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.