insert into array

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

insert into array

Post by shiznatix »

Ok i have to insert into a key and value into a array between 2 keys. i have keys 1 and 3 but after thats made i need to be able to insert a key 2 between the 2. when i do $arr['2'] = $some_var then it just inserts it after key 3 but i cant have that.
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Post by anjanesh »

array_multisort works only for numeric keys.

Code: Select all

<?php
$arr = array();
$arr[0]="a";
$arr[2]="c";
$arr[1]="b";

array_multisort($arr);

while (list($k,$v)=each($arr))
 {
        echo $k.' : '.$v.'<br>';
 }
?>
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post by shiznatix »

how can i keep the original keys? they are numeric keys
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Post by anjanesh »

Output is

0 : a
1 : b
2 : c

Original keys intact.
Post Reply