in_array is lying to me :(

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

in_array is lying to me :(

Post 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
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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>';
	}
}
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

I'm sorry, the behavior was correct. The fields it was indicating as not found are from another table. :oops:
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

Basically, you were lying to yourself ;)
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

timvw wrote:Basically, you were lying to yourself ;)
Indeed
Post Reply