onedumbcoder Posted September 4, 2009 Share Posted September 4, 2009 Lets say I have an array with 10 values in it. is there a way to check that is all values are unique and there is no duplicate?? :'( Quote Link to comment https://forums.phpfreaks.com/topic/173145-warning-only-a-super-genius-can-solve-this-array-with-unique-values/ Share on other sites More sharing options...
Philip Posted September 4, 2009 Share Posted September 4, 2009 Did you look for the proper function in the PHP manual? http://php.net/manual/en/ref.array.php Quote Link to comment https://forums.phpfreaks.com/topic/173145-warning-only-a-super-genius-can-solve-this-array-with-unique-values/#findComment-912602 Share on other sites More sharing options...
scotchegg78 Posted September 4, 2009 Share Posted September 4, 2009 would you not have to run through it putting each value into another temp array as you go, then on each step check your temp array for its content against your new entry? Quote Link to comment https://forums.phpfreaks.com/topic/173145-warning-only-a-super-genius-can-solve-this-array-with-unique-values/#findComment-912604 Share on other sites More sharing options...
onedumbcoder Posted September 4, 2009 Author Share Posted September 4, 2009 I FORGOT TO ADD THIS: that all the values are unique and all the values are digits Quote Link to comment https://forums.phpfreaks.com/topic/173145-warning-only-a-super-genius-can-solve-this-array-with-unique-values/#findComment-912605 Share on other sites More sharing options...
Philip Posted September 4, 2009 Share Posted September 4, 2009 all the values are unique and all the values are digits is there a way to check that is all values are unique and there is no duplicate?? :'( Ummm, so if you know for sure that the values are unique, why do you need to check to see if they are? And there is a built in function to do this, take a look at the link I sent you. Quote Link to comment https://forums.phpfreaks.com/topic/173145-warning-only-a-super-genius-can-solve-this-array-with-unique-values/#findComment-912610 Share on other sites More sharing options...
scotchegg78 Posted September 4, 2009 Share Posted September 4, 2009 or use kings link and look at.. array_unique() or in_array() Quote Link to comment https://forums.phpfreaks.com/topic/173145-warning-only-a-super-genius-can-solve-this-array-with-unique-values/#findComment-912613 Share on other sites More sharing options...
Maq Posted September 4, 2009 Share Posted September 4, 2009 You can check to see if the original array is the same size as if you called array_unique on it, if not, then there are dupes, if so, it's unique. Quote Link to comment https://forums.phpfreaks.com/topic/173145-warning-only-a-super-genius-can-solve-this-array-with-unique-values/#findComment-912618 Share on other sites More sharing options...
Daniel0 Posted September 4, 2009 Share Posted September 4, 2009 $unique = true; foreach ($array as $i => $elem) { foreach ($array as $j => $elem2) { if ($i != $j && $elem === $elem2) { $unique = false; break 2; } } } $unique = count(array_unique($array)) == count($array); Plenty of ways. I'm a super genius Edit: So is Maq Quote Link to comment https://forums.phpfreaks.com/topic/173145-warning-only-a-super-genius-can-solve-this-array-with-unique-values/#findComment-912620 Share on other sites More sharing options...
onedumbcoder Posted September 4, 2009 Author Share Posted September 4, 2009 how about performing a preg_match on the array? is the an easy way without having to go through an array? Quote Link to comment https://forums.phpfreaks.com/topic/173145-warning-only-a-super-genius-can-solve-this-array-with-unique-values/#findComment-912625 Share on other sites More sharing options...
Daniel0 Posted September 4, 2009 Share Posted September 4, 2009 preg_match operates on strings. Quote Link to comment https://forums.phpfreaks.com/topic/173145-warning-only-a-super-genius-can-solve-this-array-with-unique-values/#findComment-912629 Share on other sites More sharing options...
Maq Posted September 4, 2009 Share Posted September 4, 2009 If you explain exactly what you are trying to do, maybe we can suggest the best approach. Since you ignored our previous suggestions it seems we're guessing until then. I FORGOT TO ADD THIS: that all the values are unique and all the values are digits Not sure what the point of this is then? Quote Link to comment https://forums.phpfreaks.com/topic/173145-warning-only-a-super-genius-can-solve-this-array-with-unique-values/#findComment-912631 Share on other sites More sharing options...
onedumbcoder Posted September 4, 2009 Author Share Posted September 4, 2009 Please keep in mind i am severely mentally challenged and excel in retardation. so what does "preg_match operates on strings." mean? Only javas? Quote Link to comment https://forums.phpfreaks.com/topic/173145-warning-only-a-super-genius-can-solve-this-array-with-unique-values/#findComment-912633 Share on other sites More sharing options...
Garethp Posted September 4, 2009 Share Posted September 4, 2009 Danial already gave it $unique = count(array_unique($array)) == count($array); It means that preg_match only works with strings, not arrays Quote Link to comment https://forums.phpfreaks.com/topic/173145-warning-only-a-super-genius-can-solve-this-array-with-unique-values/#findComment-912634 Share on other sites More sharing options...
onedumbcoder Posted September 4, 2009 Author Share Posted September 4, 2009 I am creating a game, where to user has 10 seconds to enter 10 numbers. The challenge is that the numbers can not be the same and that the values entered are numbers and nothing else. so what gets send to the server is an array. Quote Link to comment https://forums.phpfreaks.com/topic/173145-warning-only-a-super-genius-can-solve-this-array-with-unique-values/#findComment-912635 Share on other sites More sharing options...
onedumbcoder Posted September 4, 2009 Author Share Posted September 4, 2009 alright but when i try preg_match_array, my page stops working Quote Link to comment https://forums.phpfreaks.com/topic/173145-warning-only-a-super-genius-can-solve-this-array-with-unique-values/#findComment-912640 Share on other sites More sharing options...
Garethp Posted September 4, 2009 Share Posted September 4, 2009 Use this!!! if(count(array_unique($array)) == count($array)) { //They are all unique } else { //They are not all unique } This has been said three times now Quote Link to comment https://forums.phpfreaks.com/topic/173145-warning-only-a-super-genius-can-solve-this-array-with-unique-values/#findComment-912641 Share on other sites More sharing options...
l0ve2hat3 Posted September 4, 2009 Share Posted September 4, 2009 add "only a super genius" to the title and look at all the admin responses haha Quote Link to comment https://forums.phpfreaks.com/topic/173145-warning-only-a-super-genius-can-solve-this-array-with-unique-values/#findComment-912650 Share on other sites More sharing options...
Maq Posted September 4, 2009 Share Posted September 4, 2009 add "only a super genius" to the title and look at all the admin responses haha 2? Quote Link to comment https://forums.phpfreaks.com/topic/173145-warning-only-a-super-genius-can-solve-this-array-with-unique-values/#findComment-912652 Share on other sites More sharing options...
bundyxc Posted September 4, 2009 Share Posted September 4, 2009 Do you have error handling turned on? Quote Link to comment https://forums.phpfreaks.com/topic/173145-warning-only-a-super-genius-can-solve-this-array-with-unique-values/#findComment-912654 Share on other sites More sharing options...
l0ve2hat3 Posted September 4, 2009 Share Posted September 4, 2009 add "only a super genius" to the title and look at all the admin responses haha 2? gotta choose my words wisely huh? gurus Quote Link to comment https://forums.phpfreaks.com/topic/173145-warning-only-a-super-genius-can-solve-this-array-with-unique-values/#findComment-912656 Share on other sites More sharing options...
Maq Posted September 4, 2009 Share Posted September 4, 2009 alright but when i try preg_match_array, my page stops working No one suggested that, in fact, that's not even a native method. We're trying to help you and you're doing the complete opposite. Until you're willing to cooperate and listen, you're wasting everyones' time. Quote Link to comment https://forums.phpfreaks.com/topic/173145-warning-only-a-super-genius-can-solve-this-array-with-unique-values/#findComment-912662 Share on other sites More sharing options...
bundyxc Posted September 4, 2009 Share Posted September 4, 2009 alright but when i try preg_match_array, my page stops working No one suggested that, in fact, that's not even a native method. We're trying to help you and you're doing the complete opposite. Until you're willing to cooperate and listen, you're wasting everyones' time. Agreed. I'll come back to the thread once he tries the suggested approaches. Quote Link to comment https://forums.phpfreaks.com/topic/173145-warning-only-a-super-genius-can-solve-this-array-with-unique-values/#findComment-912671 Share on other sites More sharing options...
onedumbcoder Posted September 4, 2009 Author Share Posted September 4, 2009 Alright I give up, I just spend the last 4 hours trying to figure how how to execute java code in php, not only couldn't i do it I had no luck googling it either. Can someone please suggest another way? Quote Link to comment https://forums.phpfreaks.com/topic/173145-warning-only-a-super-genius-can-solve-this-array-with-unique-values/#findComment-912713 Share on other sites More sharing options...
Daniel0 Posted September 4, 2009 Share Posted September 4, 2009 What are you talking about? Java? Quote Link to comment https://forums.phpfreaks.com/topic/173145-warning-only-a-super-genius-can-solve-this-array-with-unique-values/#findComment-912724 Share on other sites More sharing options...
onedumbcoder Posted September 4, 2009 Author Share Posted September 4, 2009 Someone said use java Only javas? Quote Link to comment https://forums.phpfreaks.com/topic/173145-warning-only-a-super-genius-can-solve-this-array-with-unique-values/#findComment-912725 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.