Jump to content

Help with php arrays and sorting


Zomie

Recommended Posts

I have a an array that uses a timestamp as the key:
[code]
Array
(
    [1164606003] => Array
        (
            [1] => 123           
            [2] => 456
            [3] => 789
        )

    [1164604202] => Array
        (
            [1] => 789
            [2] => 456
            [3] => 123
)

[/code]

I would like to do a replace on the key so it looks like this:

[code]
Array
(
    [1] => Array
        (
            [1] => 123           
            [2] => 456
            [3] => 789
        )

    [2] => Array
        (
            [1] => 789
            [2] => 456
            [3] => 123
)

[/code]

While maintaining its order, the only php functions I have found to replace the key comes with sort, which changes the order of the array.

Is there a function I am missing or an easy way to do this?  I apoligize I am fairly new to php, most of my background is in ColdFusion.
Link to comment
Share on other sites

http://sg.php.net/manual/en/function.array-merge.php

This will work, providing your array is numerically indexed (that is, as long as those are ints and not strings).  BUT, it will re-index starting with 0, not with 1.  Do you need it to start at 1 rather than at 0?

array_combine() may also be useful.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.