Page 1 of 1

in_array is lying to me :(

Posted: Sat Jul 29, 2006 12:47 pm
by Benjamin
I don't quite understand what is going on here. I have this code that captures post data and inserts it into a database query based on fields it pulls from a table with a describe query..

Code: Select all

foreach ($this->userAnswers AS $key => $value)
        {
            if (in_array($key, $this->tableFields))
            {
                $this->data[$key] = $value;
                echo 'FOUND FIELD ' . $key . "<br>";
            } else {
                echo 'COULD NOT FIND ' . $key . ' Field<br>';
            }
        }
The problem is that it's returning true on some fields and false on others even though they all exist. The case is the same, they are all text strings. They are all there :?

[userName] => myusername
COULD NOT FIND userName Field
[TravelDistance] => 55
FOUND FIELD TravelDistance

Posted: Sat Jul 29, 2006 12:53 pm
by volka
Does this version "find" more fields?

Code: Select all

$this->userAnswers = array_map('strtolower', array_map('trim', $this->userAnswers));
$this->tableFields = array_map('strtolower', array_map('trim', $this->tableFields));

foreach ($this->userAnswers AS $key => $value)
{
	if (in_array($key, $this->tableFields))
	{
		$this->data[$key] = $value;
		echo 'FOUND FIELD ' . $key . "<br>";
	} else {
		echo 'COULD NOT FIND ' . $key . ' Field<br>';
	}
}

Posted: Sat Jul 29, 2006 1:02 pm
by Benjamin
I'm sorry, the behavior was correct. The fields it was indicating as not found are from another table. :oops:

Posted: Sat Jul 29, 2006 1:12 pm
by timvw
Basically, you were lying to yourself ;)

Posted: Sat Jul 29, 2006 1:19 pm
by Benjamin
timvw wrote:Basically, you were lying to yourself ;)
Indeed