0

Here's my array

Array
(
    [_id] => MongoId Object
        (
            [$id] => 4ff6e96bb0b4599016000006
        )

    [alias] => me
    [create] => 1341581675
    [name] => It's Me!
)

I set [id] => 4ff6e96bb0b4599016000006 and unset [_id]

Now

Array
(
    [alias] => me
    [create] => 1341581675
    [name] => It's Me!
    [id] => 4ff6e96bb0b4599016000006 
)

What's the best way to order [id] on top and order [create] after [name]

Thanks

5
  • 5
    Why does it matter the order for you? Commented Jul 7, 2012 at 20:30
  • possible duplicate of Array push as the first index PHP Commented Jul 7, 2012 at 20:31
  • That is a valid case when dealing with numbered indexes, not named ones. Commented Jul 7, 2012 at 20:34
  • php.net/manual/en/function.asort.php Commented Jul 7, 2012 at 20:36
  • @MarcoAurélioDeleu designing array API key :) Commented Jul 7, 2012 at 20:43

1 Answer 1

0

Instead of setting $yourArray['id'] = '4ff6e9...0006', you could (ab)use array_merge() to add it:

$yourArray = array_merge(array('id'=>'4ff6e9...006'), $yourArray);

This should basically append your original array to the small array above which contains only the key 'id'.

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

Comments

Your Answer

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