5

I'm trying to do some kind of function that will find (in the following array) the object with the id of 2, and move it to the top of the array. Here's the original array:

Array
(
    [0] => stdClass Object
        (
            [id] => 177
            [startdate] => 2014-08-02
        )

    [1] => stdClass Object
        (
            [id] => 178
            [startdate] => 2014-08-02
        )

    [2] => stdClass Object
        (
            [id] => 2
            [startdate] => 2014-07-28
        )

    [3] => stdClass Object
        (
            [id] => 82
            [startdate] => 2014-07-28

        )

    [4] => stdClass Object
        (
            [id] => 199
            [startdate] => 2013-10-10
        )
)

And here is what I'd like it to output (with the moved array item):

Array
(

    [0] => stdClass Object
        (
            [id] => 2
            [startdate] => 2014-07-28
        )
    [1] => stdClass Object
        (
            [id] => 177
            [startdate] => 2014-08-02
        )

    [2] => stdClass Object
        (
            [id] => 178
            [startdate] => 2014-08-02
        )

    [3] => stdClass Object
        (
            [id] => 82
            [startdate] => 2014-07-28

        )

    [4] => stdClass Object
        (
            [id] => 199
            [startdate] => 2013-10-10
        )
)

Any help would be appreciated.

SoulieBaby
  • 5,405
  • 25
  • 95
  • 145
  • I just realised that the array key is also 2, I don't want it to move that, I need it to move the one which is [id] => 2.. – SoulieBaby Sep 24 '12 at 06:30
  • Do not edit question in the comments, put your edit up in the question. What have you tried already? – Glavić Sep 24 '12 at 06:33
  • Compare (when there is no need to look inside the inner array): http://stackoverflow.com/a/27217431/1333493 – Nemo Mar 30 '17 at 13:01

1 Answers1

13
function customShift($array, $id){
    foreach($array as $key => $val){     // loop all elements
        if($val->id == $id){             // check for id $id
            unset($array[$key]);         // unset the $array with id $id
            array_unshift($array, $val); // unshift the array with $val to push in the beginning of array
            return $array;               // return new $array
        }
    }
}

print_r(customShift($data, 2));
Mihai Iorga
  • 39,330
  • 16
  • 106
  • 107
  • 1
    break from foreach when you found the record OR fix the code, so the $out array can have multiple rows, and multiple rows can be unshifted. – Glavić Sep 24 '12 at 06:41
  • I agree that ID should be unique, but you never know what people are trying to do, thats why I have made OR proposal ;-) p.s. You can make your code even smaller by 2 lines, remove $out variable, and move unshift where you set $out. – Glavić Sep 24 '12 at 06:55
  • Do you think we can make it even smaller? ;-) Just joking. +1 – Glavić Sep 24 '12 at 07:01
  • This works well! when you do array_unshift it moves the array on the top of the array you want thanks +1 – Shashank Shah Aug 24 '23 at 06:45