• Checking an array for specific values
    18 replies, posted
I'm wondering if there's a way to check an array for particular values associated with certain elements (e.g. a variable holding the value 1), and if there's a way to select all elements that contain a given value (e.g. all variables which hold the value 1). This also applies to [b]PHP[/b] (crap, once again I forgot to state the language this is applicable for). Any help is greatly appreciated.
what
Without knowing what language you're talking about (assuming javascript), you could make a function that does this for you. The function would make an array to store the index's it finds in the array to be searched. It would loop through the to-be-searched array, find the value(s) you're looking for, and throw the corresponding index as a value in the new array, then the function could return the array of found indexs. I won't provide the exact code because I want you to try and make it yourself, this will increase your epic self-helping problem solving higher thinking super ultra skills of death. [editline]11:35PM[/editline] [QUOTE=turb_;21275515]what[/QUOTE] hurrrrrrrrrrrrrrrrrrrrr x 10e10000
elements = keys? PHP, JS, what?
My bad, I mean't this for PHP.
[QUOTE=livelonger12;21275957]My bad, I mean't this for PHP.[/QUOTE] Still makes little sense. Check out in_array()
You are terrible at explaining what you need.
You could do that by first making a for loop that will go through all the values of the array you want to check. In the body check do see if the array at that point is equal to whatever you are checking for, and if it is then push the value at that point into another array, and that array will hold all of the places where it is that value. I'm pretty sure that in php the arrays aren't of predetermined size, so it should work out.
JavaScript I use for blacklisting by username. [code] /* Prototype stuff Stolen from some dude named "Graham Bradley" this returns false instead of the table entry like his because 0 evals to false... */ Array.prototype.has=function(v){ for (i=0; i<this.length; i++){ if (this[i]==v) return true; }; return false; }; [/code] Usage: [code] var yourmum = ["ice cream", "chicken", "dick"]; if ( yourmum.has("dick") ) { alert("yup, your mum has a dick"); }; [/code]
That's pretty cool, although I don't think much of the example.
haha oops, I see what you meant.
No, I mean it's stupid and immature [editline]07:41PM[/editline] Oh, you didn't even write the function anyway... [code] Stolen from some dude named "Graham Bradley" [/code]
That's the point, if you are offended, it tells me that you read the manual.
[QUOTE=turb_;21283530] [editline]07:41PM[/editline] Oh, you didn't even write the function anyway... [code] Stolen from some dude named "Graham Bradley" [/code][/QUOTE] I didn't write the original function, which is this: [code] Array.prototype.has=function(v){ for (i=0; i<this.length; i++){ if (this[i]==v) return i; } return false; } [/code] This returns the item index in the array, which I don't care about, and I'd rather do a simple Boolean comparison because it looks nicer.
ok.
It's not hard to write that function. :downs:
Steering off-topic even more here, but extending Array in Javascript can cause problems when you go to iterate over one.
I greatly appreciate all help given, but I'm still unable to find a solution. In particular, I'm looking for how I can select variables with specific values within an array, and then to push them into a second array (e.g. an array for all values with 1 and another for all with 0) with the original variables associated.
It's not that hard. Look at the documentation on how to look and check. -.-
Sorry, you need to Log In to post a reply to this thread.