The Letter E Posted January 18, 2011 Share Posted January 18, 2011 Hey everybody, I have a script that I think should be working, but it's not...go figure Here's the snippet that is causing and epic fail: <?php foreach($Array as $key => $value){ $$value = $key; } $checkVal = 'someValue'; $output = isset($$checkVal) ? TRUE : FALSE; ?> As you can see it basically sets the value of the array element to a var var and then checks agains an input word. If the input word matched the varName of a set variable, we can then assume that word was in the array and return TRUE. Pretty straight forward and I've tried about 3 different approaches to this, including: in_array and flipping and checking for isset(array['value']). The array that is being checked against is usually upwards of 15000 elements. I would appreciate any knowledge that helps understand any issues in searching large arrays and good ways to get around them, or if it's just an error in my coding/logic, let me know! Thank You all in advance. E Quote Link to comment https://forums.phpfreaks.com/topic/224880-checking-large-array-for-value/ Share on other sites More sharing options...
Pikachu2000 Posted January 18, 2011 Share Posted January 18, 2011 Are you just trying to determine if a particular value exists somewhere in the array? Quote Link to comment https://forums.phpfreaks.com/topic/224880-checking-large-array-for-value/#findComment-1161523 Share on other sites More sharing options...
AbraCadaver Posted January 18, 2011 Share Posted January 18, 2011 I don't see why in_array() doesn't work: $array = range(1, 15000); $array[] = 'someValue'; $checkVal = 'someValue'; if(in_array($checkVal, $array)) { echo "FOUND"; } else { echo "NOT FOUND"; } Quote Link to comment https://forums.phpfreaks.com/topic/224880-checking-large-array-for-value/#findComment-1161525 Share on other sites More sharing options...
Pikachu2000 Posted January 18, 2011 Share Posted January 18, 2011 That's exactly what I was thinking, Abra. Quote Link to comment https://forums.phpfreaks.com/topic/224880-checking-large-array-for-value/#findComment-1161526 Share on other sites More sharing options...
The Letter E Posted January 18, 2011 Author Share Posted January 18, 2011 I don't see why in_array() doesn't work: $array = range(1, 15000); $array[] = 'someValue'; $checkVal = 'someValue'; if(in_array($checkVal, $array)) { echo "FOUND"; } else { echo "NOT FOUND"; } ok, apparently it's a problem with the ternary operator i wrote. Is it written incorrectly?? looks right to me. Ternary(doesn't work): <?php $this->output = in_array($this->word, $engArray) ? '1' : '0'; ?> Standard IF, ELSE(works): <?php if(in_array($this->word, $engArray)){ $this->output = '0'; }else{ $this->output = '1'; } ?> Any ideas...and No, I'm not completely set on using the ternary operater, but I'd prefer to. Quote Link to comment https://forums.phpfreaks.com/topic/224880-checking-large-array-for-value/#findComment-1161530 Share on other sites More sharing options...
The Letter E Posted January 18, 2011 Author Share Posted January 18, 2011 I don't see why in_array() doesn't work: $array = range(1, 15000); $array[] = 'someValue'; $checkVal = 'someValue'; if(in_array($checkVal, $array)) { echo "FOUND"; } else { echo "NOT FOUND"; } ok, apparently it's a problem with the ternary operator i wrote. Is it written incorrectly?? looks right to me. Ternary(doesn't work): <?php $this->output = in_array($this->word, $engArray) ? '1' : '0'; ?> Standard IF, ELSE(works): <?php if(in_array($this->word, $engArray)){ $this->output = '0'; }else{ $this->output = '1'; } ?> Any ideas...and No, I'm not completely set on using the ternary operater, but I'd prefer to. Actually, I accidentally flipped the values. Neither of those is working. Quote Link to comment https://forums.phpfreaks.com/topic/224880-checking-large-array-for-value/#findComment-1161531 Share on other sites More sharing options...
PFMaBiSmAd Posted January 18, 2011 Share Posted January 18, 2011 It would help if you posted an example showing what the part of your array looks like that should be matched and what your $this->word looks like, and have you actually echoed or used var_dump() on the values so that you know that they actually do match (in_array requires an exact match.) Quote Link to comment https://forums.phpfreaks.com/topic/224880-checking-large-array-for-value/#findComment-1161537 Share on other sites More sharing options...
The Letter E Posted January 18, 2011 Author Share Posted January 18, 2011 It would help if you posted an example showing what the part of your array looks like that should be matched and what your $this->word looks like, and have you actually echoed or used var_dump() on the values so that you know that they actually do match (in_array requires an exact match.) Array sample: ... [7299] => cheatrie [7300] => cheats [7301] => chebacco [7302] => chebec [7303] => chebel [7304] => chebog [7305] => chebule [7306] => chebulinic [7307] => chebyshev [7308] => chechehet [7309] => chechen [7310] => check [7311] => checkable [7312] => checkage [7313] => checkbird [7314] => checkbite [7315] => checkbook [7316] => checkbooks [7317] => checked ... $this->word will be a user input string i.e. 'check' . It can be anything, but assuming it matches an array value the script should catch that. No var_dump() is currently in place. Quote Link to comment https://forums.phpfreaks.com/topic/224880-checking-large-array-for-value/#findComment-1161552 Share on other sites More sharing options...
AbraCadaver Posted January 18, 2011 Share Posted January 18, 2011 It's got to be a problem with your data. You need to var_dump() or echo them in the function where you are trying to use them: $word = 'hi'; $engArray = array('hi', 'bye'); $output = in_array($word, $engArray) ? '1' : '0'; var_dump($output); // string(1) "1" $word = 'cya'; $engArray = array('hi', 'bye'); $output = in_array($word, $engArray) ? '1' : '0'; var_dump($output); // string(1) "0" Quote Link to comment https://forums.phpfreaks.com/topic/224880-checking-large-array-for-value/#findComment-1161557 Share on other sites More sharing options...
The Letter E Posted January 18, 2011 Author Share Posted January 18, 2011 It's got to be a problem with your data. You need to var_dump() or echo them in the function where you are trying to use them: $word = 'hi'; $engArray = array('hi', 'bye'); $output = in_array($word, $engArray) ? '1' : '0'; var_dump($output); // string(1) "1" $word = 'cya'; $engArray = array('hi', 'bye'); $output = in_array($word, $engArray) ? '1' : '0'; var_dump($output); // string(1) "0" Word, it's the data. I just ran a var_dump() on one of the array values and they all have an extra blank space at the end. I feel like a dumb, haha I'm just gonna toss an rtrim on there for now. Thanks for the help everybody : ) Quote Link to comment https://forums.phpfreaks.com/topic/224880-checking-large-array-for-value/#findComment-1161561 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.