3

here is my array:

Array
(
    [0] => Array
        (
            [id] => 8
            [title] => MUSIC
        )

    [1] => Array
        (
            [id] => 17
            [title] => Indie
        )

    [2] => Array
        (
            [id] => 14
            [title] => SPORTS
        )

    [3] => Array         // Need to move this on first position
        (
            [id] => 15
            [title] => Hipster  
        )

    [4] => Array
        (
            [id] => 16
            [title] => Web Seriesdf
        )
   )

I want array with key [3] to be on first position and then the rest of the elements. I tried array_merge and array_unshift. But not working

Aashi
  • 389
  • 1
  • 9
  • 20
  • 1
    Is your array guaranteed to be numerically indexed? Do you care about preserving the numeric indexing after moving the element? – Jon Jun 17 '16 at 09:28
  • Possible duplicate of [Move array item with certain key to the first position in an array, PHP](http://stackoverflow.com/questions/11703729/move-array-item-with-certain-key-to-the-first-position-in-an-array-php) – A J Jun 17 '16 at 09:38
  • It would be good if you could mark some answer as correct. – Tirthraj Barot Jun 17 '16 at 09:56

5 Answers5

6

You just need to take only three steps.

- Copy n'th array in variable.
- Delete n'th index from array. using unset()
- Put variable at the 0th index of the array. Using array_unshift()

Step 1:

$array=$mainArray[N];

Step 2:

unset($mainArray[N]);

Step 3:

array_unshift($mainArray, $array);
Alok Patel
  • 7,842
  • 5
  • 31
  • 47
3

Lets assume your array to be $x;

$new_value = $x[3];
unset($x[3]);
array_unshift($x,$new_value);

This shall solve your problem.

  • 1
    @AniketPandya hahaha don't be greedy you have 4 votes up already `:-p` , – Martin Jun 17 '16 at 09:34
  • 2
    http://stackoverflow.com/questions/11703729/move-array-item-with-certain-key-to-the-first-position-in-an-array-php – Abdulla Nilam Jun 17 '16 at 09:37
  • 2
    Not sure why you deleted your previous answer that was identical to this one? Perhaps because it had a comment similar to Adbulla's saying it was copy pasted? – Jon Jun 17 '16 at 09:55
2

You can also use array_merge

<?php
$a = [1,2,3,4,5];
$new_value = [$a[1]];
unset($a[1]);
$c = array_merge($new_value,$a);
print_r($c);

?>

Check output : https://eval.in/590702

Niklesh Raut
  • 34,013
  • 16
  • 75
  • 109
  • while this is a nice alternative to `array_unshift`, could you explain about how this works? – Martin Jun 17 '16 at 09:33
2

Use

$array = array('3' => $array['3']) + $array;

phpfiddle Preview and eval.in Preview

Note: im moving 2 in my array. In your method add 3


Example

$array = array(
    '0' => array(
        'id'=>'8',
        'title'=>'MUSIC'
        ),
    '1'     => array(
        'id'=> '17',
        'title'=> 'Indie'
        ),
     '2'     => array(
        'id'=>'14',
        'title'=>'SPORTS',
        ),
     '3'     => array(
        'id'=>'14',
        'title'=>'SPORTS',
        ),
    );

$array = array('2' => $array['2']) + $array;
print_r($array);

Output

Array ( [2] => Array ( [id] => 14 [title] => SPORTS ) [0] => Array ( [id] => 8 [title] => MUSIC ) [1] => Array ( [id] => 17 [title] => Indie ) [3] => Array ( [id] => 14 [title] => SPORTS ) )
Abdulla Nilam
  • 36,589
  • 17
  • 64
  • 85
1

Use array_unshift();

Please check below code.

<?php
$a = Array(
    '0' => Array('id' => '8','title' => 'MUSIC'),
    '1' => Array('id' => '17','title' => 'Indie'),
    '2' => Array('id' => '14','title' => 'SPORTS'),
    '3' => Array('id' => '15','title' => 'Hipster'),
    '4' => Array('id' => '16','title' => 'Web Seriesdf')
   );
echo '<pre>';print_r($a); 
$a3 = $a['3'];
unset($a['3']);
array_unshift($a,$a3);
echo '<pre>';print_r($a);   
?>

Check output - https://eval.in/590723

charan
  • 53
  • 4