Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    Registered User
    Join Date
    Feb 2003
    Location
    Berkshire, UK
    Posts
    50
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Can I reset array keys?

    Best explained as an example:

    $array[8] = 'Bob';
    $array[17] = 'Geoff';
    $array[2] = 'Alice';


    Can I get the above array to become this?

    $array[0] = 'Alice';
    $array[1] = 'Bob';
    $array[2] = 'Geoff';


    The names are examples... I do not want any change in the order of the array. I simply want the keys to be like this so I can access the data more easily.

    If there is not a simple solution that i have overlooked then I will post here later with more information of what I am actually trying to achieve.

    Thanks in advance

  • #2
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,040
    Thanks
    10
    Thanked 92 Times in 90 Posts
    there must be a simpler way but I am yakked if I can think of it right now ?

    PHP Code:
    <?
    $array
    [8] = 'Bob';
    $array[17] = 'Geoff';
    $array[2] = 'Alice';

    //if you want 
    //sort($array);

    $new=array_merge(array(),$array);
    print_r($new);
    ?>
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

  • #3
    Registered User
    Join Date
    Feb 2003
    Location
    Berkshire, UK
    Posts
    50
    Thanks
    0
    Thanked 0 Times in 0 Posts
    strangely the results of what you wrote do this:

    Array
    (
    [0] => Bob
    [1] => Geoff
    [2] => Alice
    )

    which is actually in the wrong order.

    $array[8] = 'Bob';
    $array[17] = 'Geoff';
    $array[2] = 'Alice';

    ^^^ with that as the original array, should it not come out as this?:

    Array
    (
    [0] => Alice
    [1] => Bob
    [2] => Geoff
    )

    the code may still work for me as the array *should* have already been loaded in the correct order. thanks again for your help

  • #4
    Regular Coder PHP6's Avatar
    Join Date
    Aug 2008
    Location
    Czech Republic
    Posts
    238
    Thanks
    18
    Thanked 34 Times in 33 Posts
    I know that post is too old but since it is in google's result for the keyword 'php reset array keys' I have decided to add the solution to help others

    The first idea which came to my mind was this:
    PHP Code:
    $threads = array('10'=>'one''2'=>'two''15'=>'three');
    $keys range(0count($threads)-1);
    $values array_values($threads);
    $threads array_combine($keys$values); 
    but later I have discovered that there is even more simple way
    PHP Code:
    $threads = array('10'=>'one''2'=>'two''15'=>'three');
    $threads array_values($threads); 
    Good luck with your script

  • #5
    New Coder
    Join Date
    Sep 2008
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks PHP6, I was looking for that.

  • #6
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    array_merge
    Given a single array, it will reindex the values for you as long as they are integer based.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #7
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,642
    Thanks
    2
    Thanked 405 Times in 397 Posts
    If your array is out of order, like the OP was talking about, run ksort() on it before using array_values() or array_merge().


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •