Jump to content

checking LARGE ARRAY for value


The Letter E

Recommended Posts

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.)

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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"

Link to comment
Share on other sites

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 : )

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.