简体   繁体   中英

PHP: sort an object on the index positions of one of its property values

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

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

As I understand your question after sorting an array by some property you want to rotate the array so that eg 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"
}

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM