Is there a php function which checks to see if all values in an array are unique? I know array_unique() will get rid of duplicates, but what about checking to be sure two different options aren't both set to the same number, for example. (in a case where you must order something 1-10 for example, want to force it to not have multiple ones with the same ordered number).
Unique In Array
Started by BASHERS33, May 02 2009 07:28 PM
11 replies to this topic
#1
Posted 02 May 2009 - 07:28 PM
|
|
|
#2
Posted 02 May 2009 - 08:39 PM
Im a bit confused. Are you trying to put things in an array based on there "id" name thing if you know the id of the array you could just check that one - if im misunderstanding you then you could just loop through the entire array to check if its a duplicate - if its order to a search on it etc
#3
Posted 02 May 2009 - 08:46 PM
Basically it's based on dropdowns (it's not ordering, I was just using that as an example) and the dropdowns all have the same choices, but I want to be sure someone doesn't select the same choice for more than one of the dropdowns. So technically it's not even an array. I just thought I may have to put it in an array to check it.
I guess I could simply put them one by one into an array via a foreach and then use
So I suppose I could do that, but I just wondered if anything already existed which would be quicker. For example in_array() is one type of check. Just figured there may be something similar to that, but instead of checking to see if a value is in an array actually be something which checks for duplicate values.
I guess I could simply put them one by one into an array via a foreach and then use
if( isset( $array[ $something ] ) ){ and have $something be the value and if ti exists, error out, if it doesn't exist then create it with the key being the value $something.So I suppose I could do that, but I just wondered if anything already existed which would be quicker. For example in_array() is one type of check. Just figured there may be something similar to that, but instead of checking to see if a value is in an array actually be something which checks for duplicate values.
#4
Guest_Jordan_*
Posted 03 May 2009 - 05:46 AM
Guest_Jordan_*
I don't think so. You can do a array_unique and then run the new array generated vs the old array with array_diff. This will give you an array with all the duplicate values.
#5
Posted 03 May 2009 - 08:41 AM
ok thanks. yeah that could work or I could use array_unique and then compare count() for each one. So I guess several ways to go.
#7
Posted 03 May 2009 - 09:09 AM
It's not going to be big ever. The biggest it would ever even possibly be would be 65 elements I think.
But anyway that was why I was asking also because if I ever need to do this somewhere where it could be a big set of data then I was hoping there was a good way to do it.
I am worried that some other unrelated areas will use too much memory as it is, but I've worked hard to try my best on it so if it happens then it happens and I would have to figure something out then.
But anyway that was why I was asking also because if I ever need to do this somewhere where it could be a big set of data then I was hoping there was a good way to do it.
I am worried that some other unrelated areas will use too much memory as it is, but I've worked hard to try my best on it so if it happens then it happens and I would have to figure something out then.
#8
Guest_Jordan_*
Posted 03 May 2009 - 12:14 PM
Guest_Jordan_*
I am curious, why do you want to "know" the duplicates and why don't you just prevent them from occuring?
#9
Posted 03 May 2009 - 12:32 PM
The whole point of this is I do want to prevent them. But since each dropdown has the same choices I have to obviously check their choices to be sure none are duplicates. How else can i prevent them if don't first know they exist? I'm not trying to get the duplicate values, I am trying to prevent them only. But how can I compare them without first getting them?
I think I may just make some multi-select box. But then again someone could mess with the url and put the same choice in twice that way also.
I think I may just make some multi-select box. But then again someone could mess with the url and put the same choice in twice that way also.
#10
Guest_Jordan_*
Posted 03 May 2009 - 01:41 PM
Guest_Jordan_*
#11
Posted 03 May 2009 - 02:03 PM
But array_unique would simply toss out the duplicates and proceed wouldn't it? I need it to error out and force them to go back and change it.
#12
Guest_Jordan_*
Posted 03 May 2009 - 02:16 PM
Guest_Jordan_*


Sign In
Create Account


Back to top










