2

I have this result from var_dump for a multidimensional array:

array (size=6)
  'sambalpur.in.net' => 
    array (size=2)
      'classkey' => string 'indotnet' (length=8)
      'status' => string 'available' (length=9)
  'sambalpur.com' => 
    array (size=2)
      'classkey' => string 'domcno' (length=6)
      'status' => string 'regthroughothers' (length=16)
  'sambalpur.info' => 
    array (size=2)
      'classkey' => string 'dominfo' (length=7)
      'status' => string 'regthroughothers' (length=16)
  'sambalpur.net' => 
    array (size=2)
      'classkey' => string 'dotnet' (length=6)
      'status' => string 'regthroughothers' (length=16)
  'sambalpur.biz' => 
    array (size=2)
      'classkey' => string 'dombiz' (length=6)
      'status' => string 'available' (length=9)
  'sambalpur.in' => 
    array (size=2)
      'classkey' => string 'dotin' (length=5)
      'status' => string 'regthroughothers' (length=16)

Now say I want to shift this specific array to the beginning of array:

'sambalpur.biz' => 
        array (size=2)
          'classkey' => string 'dombiz' (length=6)
          'status' => string 'available' (length=9)

I have tried:

array_unshift($array,array('sambalpur.biz'));

But what I am getting is like this:

array (size=7)
  0 => 
    array (size=1)
      0 => string 'sambalpur.biz' (length=13)
  'sambalpur.in.net' => 
    array (size=2)
      'classkey' => string 'indotnet' (length=8)
      'status' => string 'available' (length=9)
  'sambalpur.com' => 
    array (size=2)
      'classkey' => string 'domcno' (length=6)
      'status' => string 'regthroughothers' (length=16)
  'sambalpur.info' => 
    array (size=2)
      'classkey' => string 'dominfo' (length=7)
      'status' => string 'regthroughothers' (length=16)
  'sambalpur.net' => 
    array (size=2)
      'classkey' => string 'dotnet' (length=6)
      'status' => string 'regthroughothers' (length=16)
  'sambalpur.biz' => 
    array (size=2)
      'classkey' => string 'dombiz' (length=6)
      'status' => string 'available' (length=9)
  'sambalpur.in' => 
    array (size=2)
      'classkey' => string 'dotin' (length=5)
      'status' => string 'regthroughothers' (length=16)

What is the correct way to shift the array?

1
  • array_unshift is for adding additional elements to an array, not rearranging it. Commented Oct 28, 2016 at 20:16

3 Answers 3

3

I thought I had done this before but couldn't find a duplicate:

$array = array_splice($array,
                      array_search('sambalpur.biz', array_keys($array)), 1) + $array;
  • Get a numerically indexed array of the keys with array_keys()
  • Search the returned array for sambalpur.biz with array_search()
  • Use the returned index to cut out that element with array_splice()
  • Add that to the existing array

Along the same line as Don't Panic:

$array = array_merge(array('sambalpur.biz' => $array['sambalpur.biz']), $array);

No need to unset as the order of insertion dictates which key overwrites the other so this one overwrites the previous one.

Sign up to request clarification or add additional context in comments.

2 Comments

Your last suggestion is much much better.
Ah, good catch. I did feel like I was missing something with the array_merge suggestion.
3

You can uksort the array, using the specific key you want to move to the beginning in the comparison function.

$key = 'sambalpur.biz';
uksort($array, function($a, $b) use ($key) {
    if ($a == $key) return -1;
    if ($b == $key) return 1;
    return 0;
});

This should move that item to the beginning without changing the order of the other items.


Another possibility is to remove the child array, and then merge the main array back onto it.

$key = 'sambalpur.biz';
$x = $array[$key];
unset($array[$key]);
$array = array_merge([$key => $x], $array);

1 Comment

I have already tested your first suggestion, it is working fine. Let me check the second one.
1

The issue seems to stem from array_unshift() reindexing elements you are passing. If you wanted to prepend your second array to your first array though and preserve indexes, you can use the + operator ($firstArray = $secondArray + $firstArray);

2 Comments

But first you need to get $secondArray out of $firstArray.
Huh, just realized that the OP wanted to rearrange his array and not prepend new elements. Got confused since he was using array_unshift(). In that case, one liner by @AbraCadaver should do the trick.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.