1

I am using a PDO approach to get an array out of database:

$statement = $db->prepare("SELECT sname FROM list WHERE ongoing = 1;");
$statement->execute();
$snames = $statement->fetchAll(PDO::FETCH_COLUMN, 0);
var_dump($snames);

The dump output is (total 2500 results):

[69]=> string(13) "ah-my-goddess"

[70]=> string(17) "ahiru-no-oujisama"

[71]=> string(13) "ahiru-no-sora"

Then I check if array $snames contains the new element $sname:

$sname = current(explode(".", $href_word_array[count($href_word_array)-1]));
if (in_array($sname, $snames) == False)
{
    echo "New '$sname'!<br>";
}
else
{
    echo "$sname is already in the list. Excluding.<br>";
    unset($snames[$sname]);
}

And the output is:

'ah-my-goddess' is already in the list. Excluding.

New 'ahiru-no-oujisama'!

'ahiru-no-sora' is already in the list. Excluding.

Why does it says that 'ahiru-no-oujisama' is the new name? We can see from the DUMP function that the array contains this element.

I have compared the results a thousand times. Notepad finds both names. There are no spaces. Name in the database is the same as in variable..

For the record - I have around 2500 entities in $snames array and for 95% of records (+-) I am getting the "already exists" result. However, for some I am getting "new".

Is that perhaps some kind of encoding issue? For the table I have DEFAULT CHARSET=latin1. Could that be a problem?

Edit

It was suggested that I added a trim operation:

$snames = $statement->fetchAll(PDO::FETCH_COLUMN, 0);
for ($i=0; $i < Count($snames); $i+=1)
{
    $snames[$i] = trim($snames[$i]);
}

and:

if (in_array(trim($sname), $snames) == False)

However I get the same problem.

8
  • i suggest to trim both...and try again Commented Apr 18, 2014 at 20:52
  • You're seeking an array value that matches, correct? And not an array key that matches? You seem to be using $sname as an array key if it exists, maybe you mean to use array_key_exists()? Commented Apr 18, 2014 at 20:56
  • @JasonOOO used trim on all items in array and on the new name. Still not able to find an element. See edit. Commented Apr 18, 2014 at 21:00
  • can you provide real data in a text file? but should be exactly same copy that you are working on them Commented Apr 18, 2014 at 21:03
  • 1
    To debbug, try to test $snames[70] == $name Commented Apr 18, 2014 at 21:10

1 Answer 1

1

Apparently, the problem was with line:

unset($snames[$sname]);

for some entries I had names such as "70" and "111"

as the result command:

unset($snames[$sname]);

removed elements at that position. Not the elements with such keys!! I.e. That's how program understood it:

unset($snames[77]);

and that's what I was expecting:

unset($snames['77']);

so the line had to be changed to following:

if(($key = array_search($sname, $snames)) !== false)
{
    unset($snames[$key]);
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.