EM-Creations
EM-Creations

Reputation: 4301

in_array() returning false, although I'm sure the needle is in the haystack

I'm having a bizarre problem with in_array. For some reason it's returning false on what I'm sure should return true.

The only reason I can think it's doing this is if there's a hidden character or something which I can't see. But I haven't added anything like this while using the variable.

Screen shot of output:

Output

isValidAnswer method:

    /**
     * Return whether this is a valid answer or not
     * 
     * @param mixed $answer
     * @return boolean $validAnswer
     */
    public function isValidAnswer($answer) {
        print($answer . "<br />"); print_r($this->answers);
        return in_array($answer, $this->answers);
    }

Any ideas? Thanks.

Upvotes: 0

Views: 1458

Answers (3)

Ripa Saha
Ripa Saha

Reputation: 2540

problem is in your array $this->answers. it's really very tough to find where is the problem. So You can try to redefine your array. I have also tried this one for below two

1) $answers = array("Yes","No","Maybe","");

2) $answers=array("Yes","No","Maybe","");

point 1 not works but point 2 works fine.

I have wrote the below for testing

function isValidAnswer($val) 
{
    $answers=array("Yes","No","Maybe","");
    //print_r($answers);
    if(in_array($val,$answers))
    {
        echo $val;
    }

}


isValidAnswer('No');

Upvotes: 1

EM-Creations
EM-Creations

Reputation: 4301

Thank you for the help but I believe I've resolved my own issue.

In the construct for the class I've added this code:

$this->answers = unserialize($pollData['answers']); // Unserialise the answers

foreach ($this->answers as $key=>$var) { // Ensure there's no hidden characters or whitespace
        preg_replace('/[\x00-\x1F\x80-\xFF]/', "", $this->answers[$key]);
        $this->answers[$key] = trim($this->answers[$key]);
}

There was some white space, and I believe a carriage return left in there. So this code in the construct should ensure from now on that it's in a "clean" format. And it's now returning true.

I also need to take a look and make sure that it's cleaned before it goes into the database.

Thank you for your help however.

Upvotes: 2

The Surrican
The Surrican

Reputation: 29866

From what is displayed here, your code is fine.

Your explanation with the characters is possible.

Your question does not show the acutal return value of the function. Only 'Error: invalid answer'. Maby the error is where you pick up the return value?

The following code outputs

No<br />Array
(
    [0] => Yes
    [1] => No
    [2] => Maybe
    [3] => 
)
bool(true)

..

<?

class Test {

     private $answers = array('Yes','No','Maybe','');

    /**
     * Return whether this is a valid answer or not
     * 
     * @param mixed $answer
     * @return boolean $validAnswer
     */


    public function isValidAnswer($answer) {
        print($answer . "<br />"); print_r($this->answers);
        return in_array($answer, $this->answers);
    }
}

$test = new Test();
var_dump($test->isValidAnswer('No'));

Upvotes: 2

Related Questions