-1

I have an array:

[photos] => Array
(
      [0] => stdClass Object
        (
          [id] => 14
          [name] => image1.jpg
        )
      [1] => stdClass Object
        (
          [id] => 24
          [name] => image2.gif
        )
)

And I would like to move element with ID =24 to the top (to be first) in array.

For example:

function mysort(24) {
...
}

And get as output:

[photos] => Array
(
     [1] => stdClass Object
        (
              [id] => 24
              [name] => image2.gif
        )          
     [0] => stdClass Object
        (
              [id] => 14
              [name] => image1.jpg
        )
)    
XTRUST.ORG
  • 3,280
  • 4
  • 34
  • 60

6 Answers6

1

Pretty straightforward. Untested but should work. I assume you just want to move one element to the top.

function mysort($array, $putOnTopId) {
    $returnArray = array();

    foreach ($array as $item) {
        if ($item->id == $putOnTopId) {
            array_unshift($returnArray, $item); // Put on top
        } else {
            array_push($returnArray, $item); // Put on bottom
        }
    }

    return $returnArray;
}
Johan Palmfjord
  • 671
  • 4
  • 15
0

I hope this will work fou you.

    function mysort(placedTOFirst)
    {
         $tempArray = array();
        $sortedarray = array()
        foreach($photosArray as $key=>$val)
        {
            if($val->id == placedTOFirst)
            {
              $sortedarray[] = $val;
            }
            else
           {
              $tempArray[] = $val;
            }
     }
     $sortedarray = array_merge($sortedarray,$tempArray);
     return $sortedarray;
}
Vishal Patel
  • 1,715
  • 16
  • 26
0

I understand what you want, but the output you show doesn't change anything. array[0] still contain id:14, and array[1] still contain id:24 ...

if you want to put the content of array[1] to array[0] and automatically translate the content of array[0] to array[1], you can check this link who show some php function who will help you to sort your array as expected. http://www.w3schools.com/php/php_ref_array.asp

0

You can make use of array_multisort (PHP 4, PHP 5) and array_map (PHP 4 >= 4.0.6, PHP 5)

array_multisort( array_map(function($_){return $_->id;},$array["photos"]), SORT_DESC, $array["photos"]);

If you have PHP 5 >= 5.5.0 then use array_column, here json_encode and json_decode are used to convert stdClass to array, so personally I prefer to use first solution using array_map.

array_multisort( array_column(json_decode(json_encode($array["photos"]), true),"id"), SORT_DESC, $array["photos"]);

Test

[akshay@localhost tmp]$ cat test.php 
<?php

$array = array("photos" => array( (object)array("id" => 14,"name" => "image1.jpg"),(object)array("id"=>24,"name"=>"image2.gif") ) );

print_r($array);

array_multisort( array_map(function($_){return $_->id;},$array["photos"]), SORT_DESC, $array["photos"]);

print_r($array);


?>

Output

[akshay@localhost tmp]$ php test.php 
Array
(
    [photos] => Array
        (
            [0] => stdClass Object
                (
                    [id] => 14
                    [name] => image1.jpg
                )

            [1] => stdClass Object
                (
                    [id] => 24
                    [name] => image2.gif
                )

        )

)
Array
(
    [photos] => Array
        (
            [0] => stdClass Object
                (
                    [id] => 24
                    [name] => image2.gif
                )

            [1] => stdClass Object
                (
                    [id] => 14
                    [name] => image1.jpg
                )

        )

)
Akshay Hegde
  • 16,536
  • 2
  • 22
  • 36
0
function mysort($id)
{
    $array = array(
                'photos' => array(
                            '1' => array(
                                     'id' => 24,
                                     'name' => 'image2.gif'
                                ),         
                            '0' => array(
                                     'id' => 14,
                                     'name' => 'image1.jpg'
                               ),
                            '2' => array(
                                     'id' => 4,
                                     'name' => 'image2.gif'
                                ),         
                            '3' => array(
                                     'id' => 34,
                                     'name' => 'image1.jpg'
                               ),
                            '4' => array(
                                     'id' => 44,
                                     'name' => 'image2.gif'
                                ),         
                            '5' => array(
                                     'id' => 54,
                                     'name' => 'image1.jpg'
                               )
                )    
            );

    foreach ($array as $key => $value) {        
        foreach ($value as $k => $v) {
            if ($v['id'] === $id) {
                $temp = $array[$key][$k];
                unset($array[$key][$k]);
                array_unshift($array[$key], $temp);
            }
        }
    }
    echo '<pre>';
    print_r($array);
}

mysort(54);
sathish kumar
  • 116
  • 1
  • 8
0

Instead of making any function I'll simply use built in function uasort of PHP as

uasort($array['photos'], function($a,$b){
    return $b->id - $a->id;
});
Narendrasingh Sisodia
  • 21,247
  • 6
  • 47
  • 54