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
https://forums.phpfreaks.com/topic/28600-help-with-php-arrays-and-sorting/
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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.