How to rename indexes?

Hi guys,

I have a PHP array and it has contents like this below
Please note indexes are from MySQL records.


Array
(
    [12] => 'john'
    [11] => 'george'
    [15] => 'karen'
    [14] => 'joseph'
)

Now I want to rename the same indexes from MySQL in ascending order so it will look like this below,


ID  | name
----------------
11 |  george
12 |  john
15 |  joseph
16 |  karen

How to interchange the indexes?

Thanks in advance.

I think that id should be a Primary Key and the table->name should be indexed



   $sql = 'SELECT id, name from ' . $tableName .' ORDER BY id ASC';
   $obj = mysql_query($sql);
   while ($row = mysql_fetch_object($obj)):
      echo  $row->id .' ==> ' , $row->name;
   endwhile;	    	
   // results
    12   ==> 'john'
    11   ==> 'george'
    15   ==> 'karen'
    14   ==> 'joseph'


   $sql =  'SELECT id, name from ' . $tableName .' ORDER BY name ASC'; 
   $obj = mysql_query($sql);
   while ($row = mysql_fetch_object($obj)):
      echo  $row->id .' ==> ' , $row->name;
   endwhile;	    	
   // results
    11   ==> 'george'
    12   ==> 'john'
    14   ==> 'joseph'
    15   ==> 'karen'


By the way, MYSQL will be dropped in the near future so it is advisable to learn PDO instead.

@John_Betong ;

Thanks man but that’s not what I’m looking for.
What I want is to interchange the ids.

If the id is 11 and I want to change it to 12 and so on…
How to do this?

thanks in advance.

I think I understand. Try sorting the array items:

// asort



echo '<pre>';

// Create array of items
$items = array
(
    '12' => 'john',
    '11' => 'george',
    '15' => 'karen',
    '14' => 'joseph',
);

// Before Sorting:
print_r($items);
// Output
Array
(
    [12] => john
    [11] => george
    [15] => karen
    [14] => joseph
)

// After Sorting:
asort($items);
print_r($items);
// Output
$items = array
(
    [11] => george
    [12] => john
    [14] => joseph
    [15] => karen
)


What I thought you meant was swap (exchange) two item keys in the array

[ot] //===============================
//
// function to swap two array item key values.
//
// usage:
// $items = swapkeys($items, ‘12’,‘11’);
//
//===============================
function swapkeys($items, $key_1, $key_2)
{
$tmp = $items[ $key_1 ];
$items[ $key_1 ] = $items[ $key_2 ];
$items[ $key_2 ] = $tmp;

return $items;
}

[/ot]

Still not right.
Only the ids must change/rename not the values.
But anyway thanks, I’ll try to figure it out myself.

may i ask why?

perhaps there is some other way to achieve your ultimate objective

@r937 ;

Okay here is the scenario.

Inside an Admin Control Panel.
An admin has the right to/can re-order the records.
Example:

Noticed you can re-order the records by dragging them up and down. (This is the same feature in the Admin Panel).

And on the public page these records will be displayed in ascending order (A must).

So as you can see my codes should fit this logic,
It should rename the indexes in ascending order base from an Admin re-ordered records choice.

Is renaming Primary Indexes illegal?
Is there any easy way to implement this without renaming the indexes?
How about creating new table? etc…?

Thanks in advance.

i didn’t see anything that i could drag, sorry

i think i understand what you’re trying to do, but i would recommend strongly against trying to renumber the id values

(note: don’t call them indexes, they are actually primary key values)

use an additional column, called something like sort_sequence, and manipulate that instead

suggestion: use FLOAT for sort_sequence

that way, when you want to move an item so that it will come between 3 and 4, just update its sort_serquence value to 3.5

if you want another one to move in between the item with 3 and the item with 3.5, just update its value to 3.25

you can keep doing this almost indefinitely with floating point numbers

just don’t do it to the id values :slight_smile:

@r937 ;

i didn’t see anything that i could drag, sorry

It’s the small table on the right side, just hover your mouse on the table and drag it up and down.

Anyway I’ll follow your advice, Thanks.