Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
how can I delete an element of an array and correct the keys?
Old 04-13-2005, 02:58 AM how can I delete an element of an array and correct the keys?
Recrehal's Avatar
Experienced Talker

Posts: 39
Location: Aachen, Germany
Trades: 0
I'm using unset($my_array[$i]) to delete an element in this array, but I don't know how to get the keys propperly again. After deleting the element the array looks like this:

Array ( [0] => 0 [1] => 1 [2] => 3 [3] => 4 [4] => 5 [5] => 6 )


As you can see the key 2 is not given anymore which gives me terrible headaches. I'd like to get normal keys back again which start at 0 and end at 5.


Now I'm sure there is a normal function for this, but I just can't find it in the manual.
Recrehal is offline
Reply With Quote
View Public Profile Visit Recrehal's homepage!
 
 
Register now for full access!
Old 04-13-2005, 05:28 AM
leavethisplace's Avatar
Ultra Talker

Posts: 297
Trades: 0
Not sure there is a function to do what you're asking.

An alternative could be to use a loop with a new array, going through the original array, pushing the wanted values into the new array and just ignoring the unwanted value?

That's what you asking right? How to keep the original order of keys, but still removing the element... ?
__________________
A lie gets halfway around the world before the truth has a chance to get its pants on. - Sir Winston Churchill

Please visit my sites:
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
leavethisplace is offline
Reply With Quote
View Public Profile
 
Old 04-13-2005, 06:43 AM
0beron's Avatar
Defies a Status

Posts: 1,832
Location: Somewhere else entirely
Trades: 0
What does the rest of your script do with the array? Is it possible that instead of unsetting parts of it, you could just set them to -1 or some other special value thatyour script would interpret as meaning 'don't use this one'?

ie your array above would look like
Array ( [0] => 0 [1] => 1 [2] => -1 [3] => 3 [4] => 4 [5] => 5 )
__________________
UPDATE 0beron SET talkupation = talkupation + lots WHERE post = 'helpful';

Please login or register to view this content. Registration is FREE
(aka MSN handwriting for forums)
0beron is offline
Reply With Quote
View Public Profile Visit 0beron's homepage!
 
Old 04-13-2005, 07:24 AM
leavethisplace's Avatar
Ultra Talker

Posts: 297
Trades: 0
now that's bloody clever!
__________________
A lie gets halfway around the world before the truth has a chance to get its pants on. - Sir Winston Churchill

Please visit my sites:
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
leavethisplace is offline
Reply With Quote
View Public Profile
 
Old 04-13-2005, 09:05 AM
Recrehal's Avatar
Experienced Talker

Posts: 39
Location: Aachen, Germany
Trades: 0
That's not clever - that's a workaround with lots of possible errors O_o Whenever possible you should rather consider copying the whole array and redefine everything by hand than inserting special characters. These special characters have effects on different behaviours of the array, the total count for example, and you'll also need to take extra care for this special character, not to mention the extra efford you'll have when you actually want to use that special character.

I found the array_merge() function to do exactly what I want.

Quote:
If only one array is given and the array is numerically indexed, the keys get reindexed in a continuous way.
So here is what you do:
PHP Code:
unset($my_array[$i]); //delete one element. The key is deleted and accesing it will output ' '
//output would now be Array ( [0] => 0 [1] => 1 [2] => 3 [3] => 4 [4] => 5 [5] => 6 ) 
 
$my_array array_merge($my_array); //reassign the keys in continous order
print_r(array_keys($my_array)); //Array ( [0] => 0 [1] => 1 [2] => 2 [3] => 3 [4] => 4 [5] => 5 ) 
Note that you might need to adjust your code to the reduced length of this array. To avoid that you can always use count($my_array)
Recrehal is offline
Reply With Quote
View Public Profile Visit Recrehal's homepage!
 
Old 04-13-2005, 10:08 AM
leavethisplace's Avatar
Ultra Talker

Posts: 297
Trades: 0
Did you just... answer your own question?
__________________
A lie gets halfway around the world before the truth has a chance to get its pants on. - Sir Winston Churchill

Please visit my sites:
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
leavethisplace is offline
Reply With Quote
View Public Profile
 
Old 04-13-2005, 02:11 PM
Novice Talker

Posts: 9
Trades: 0
Simpledoo. There's a function called array_values which simply takes an array and returns all the values back in a properly enumerated array.

PHP Code:
 $arr array_values ($arr); 
MarkyH is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to how can I delete an element of an array and correct the keys?
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.20137 seconds with 12 queries