Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 04-06-2003, 04:43 PM   PM User | #1
thox
Registered User

 
Join Date: Feb 2003
Location: Berkshire, UK
Posts: 50
Thanks: 0
Thanked 0 Times in 0 Posts
thox is an unknown quantity at this point
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
thox is offline   Reply With Quote
Old 04-06-2003, 05:36 PM   PM User | #2
firepages
Super Moderator


 
Join Date: May 2002
Location: Perth Australia
Posts: 3,942
Thanks: 7
Thanked 82 Times in 81 Posts
firepages will become famous soon enough
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)
firepages is offline   Reply With Quote
Old 04-06-2003, 06:49 PM   PM User | #3
thox
Registered User

 
Join Date: Feb 2003
Location: Berkshire, UK
Posts: 50
Thanks: 0
Thanked 0 Times in 0 Posts
thox is an unknown quantity at this point
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
thox is offline   Reply With Quote
Old 08-25-2008, 01:00 PM   PM User | #4
PHP6
Regular Coder

 
PHP6's Avatar
 
Join Date: Aug 2008
Location: Czech Republic
Posts: 234
Thanks: 17
Thanked 34 Times in 33 Posts
PHP6 is on a distinguished road
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
PHP6 is offline   Reply With Quote
Old 02-02-2009, 08:28 AM   PM User | #5
rushhh
New Coder

 
Join Date: Sep 2008
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
rushhh is an unknown quantity at this point
Thanks PHP6, I was looking for that.
rushhh is offline   Reply With Quote
Old 02-02-2009, 06:00 PM   PM User | #6
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,738
Thanks: 4
Thanked 2,464 Times in 2,433 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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'); 
Fou-Lu is offline   Reply With Quote
Old 02-03-2009, 06:55 AM   PM User | #7
Inigoesdr
Super Moderator


 
Inigoesdr's Avatar
 
Join Date: Mar 2007
Location: Florida, USA
Posts: 3,604
Thanks: 2
Thanked 399 Times in 392 Posts
Inigoesdr is a jewel in the roughInigoesdr is a jewel in the roughInigoesdr is a jewel in the rough
If your array is out of order, like the OP was talking about, run ksort() on it before using array_values() or array_merge().
Inigoesdr is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 07:58 AM.


Advertisement
Log in to turn off these ads.