I'm new to PHP!
I am writing a script to sort new entries pasted into a hosts file into ascending IPv4 address order. The script works fine, and (to my surprise) eliminates any duplicate entries.
I say 'to my surprise' because I haven't (yet) written any explicit code to eliminate duplicate entries; the ksort function (operating on the decimal numerical value of the IP addresses as keys) seems to take care of this automatically.
Should I have expected this? Or am I just lucky?! Always nervous of the unexpected, even if it's good news!
With thanks.
SOLVED: ksort
Moderator: General Moderators
-
AdrianK_IT
- Forum Newbie
- Posts: 4
- Joined: Tue Dec 13, 2011 8:53 am
SOLVED: ksort
Last edited by AdrianK_IT on Thu Feb 23, 2012 9:48 am, edited 1 time in total.
Re: ksort
If is always difficult to answer hypothetical questions. That said, you should post the piece of code in questions so the rest of us along with the experts can take a look at it and be able to give you a better more intelligent answer. Right now, without looking at your code we would guessing.
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
Re: ksort
If you are building an array of values and you use a duplicate array key you are in effect overwriting any previous data stored in that array member. All ksort() is going to do is order your array for you by key. Its not going to do anything else to your array. So if you were to inspect your array prior to running it through ksort() you'd see that you have unique entries simply because new entries with the same key value overwrite previous entries with the same key value.
-
AdrianK_IT
- Forum Newbie
- Posts: 4
- Joined: Tue Dec 13, 2011 8:53 am
SOLVED Re: ksort
Thanks for your help on this.
I realise now that the elimination of duplicate entries (overwriting) is being carried out by an array_combine function in my code, operating as I would expect.
I understand the reasons for it (so I'm not complaining), but the requirement for new members to complete a captcha for every post (even previews) makes posting code impractical for me. I have poor eyesight; captchas are a nightmare. Yet, as an inexperienced coder, I can't risk posting code without previewing how it appears. I could waste a lot of other people's time if errors creep in.
I realise now that the elimination of duplicate entries (overwriting) is being carried out by an array_combine function in my code, operating as I would expect.
I understand the reasons for it (so I'm not complaining), but the requirement for new members to complete a captcha for every post (even previews) makes posting code impractical for me. I have poor eyesight; captchas are a nightmare. Yet, as an inexperienced coder, I can't risk posting code without previewing how it appears. I could waste a lot of other people's time if errors creep in.
-
AdrianK_IT
- Forum Newbie
- Posts: 4
- Joined: Tue Dec 13, 2011 8:53 am
Re: SOLVED: ksort
PS I have posted the code at http://ethinks.org/ksort.htm