-1

i have two array. The first with my objects :

Array
(
[0] => stdClass Object
    (
        [id_keyword] => 148
        [id_group] => 34
    )

[1] => stdClass Object
    (
        [id_keyword] => 200
        [id_group] => 34
    )

[2] => stdClass Object
    (
        [id_keyword] => 151
        [id_group] => 34
    )

[3] => stdClass Object
    (
        [id_keyword] => 207
        [id_group] => 34
    )
)

And another one, with the position i need based on id_keyword :

Array
(
   [0] => 200
   [1] => 148
   [2] => 151
   [3] => 207
)

I want to sort my first array. I look on array_multisort, but didn't make it work :/

Any idea ?

Thanks !

  • This is quite a peculiar and specific ordering algorithm, i guess you'll have to design your own function to achieve what you want. – STT LCU Sep 23 '13 at 10:15
  • possible duplicate of [Reference: all basic ways to sort arrays and data in PHP](http://stackoverflow.com/questions/17364127/reference-all-basic-ways-to-sort-arrays-and-data-in-php) – deceze Sep 23 '13 at 10:16

1 Answers1

1

In that case, if I could, I would use the id_keyword value as the key of the first array. So, when creating the object, it now looks something like

<?php
  $my_object = new stdClass();
  $my_object->id_keyword = $a_value;
  $my_object->.... other variables here

  // and when the object is added in the array
  $my_array[] = $my_object;
?>

Instead of that last line, I suggest

<?php
  $my_array[$my_object->id_keyword] = $my_object;
?>

Then use ksort to sort your array.

Only other way I can think of, is to implement your own sort with usort.

mavrosxristoforos
  • 3,573
  • 2
  • 25
  • 40