PHP in_array函数保持匹配不存在的值

Below I have PHP Code that...
- Iterates over the User Array
- Builds an HTML Selection list of ALL Users
- If a User from the $subscriberIdFromDb Array exist in the Array of all users, then it adds a selected attribute to the HTML Selection element.
- It the above criteria is met, it also adds a Hidden Form field element with the ID of the User.

Now in my $subscriberIdFromDb array below you can see 4 test users. However when I run the code below, it always seems to add a 5th user with the ID of number 1.

I cannot figure out why this 5th user is getting added, I exported my Database results to these test Arrays and tried on a different system and it still does it, so it does seem to be something in this code causing the problem.

Any ideas what is causing this problem?

PHP Array Returned from my Database result of ALL Users in the test system...

<?php
$userArray = array (
  'seed_chris_id' => 
  array (
    'id' => 'seed_chris_id',
    'user_name' => 'chris',
    'first_name' => 'Chris',
    'last_name' => 'Olliver',
  ),
  1 => 
  array (
    'id' => '1',
    'user_name' => 'jasondavis',
    'first_name' => 'Jason',
    'last_name' => 'Davis',
  ),
  '1702c3d0-df12-2d1b-d964-521becb5e3ad' => 
  array (
    'id' => '1702c3d0-df12-2d1b-d964-521becb5e3ad',
    'user_name' => 'Jeff',
    'first_name' => 'Jeff',
    'last_name' => 'Mosley',
  ),
  'seed_jim_id' => 
  array (
    'id' => 'seed_jim_id',
    'user_name' => 'jim',
    'first_name' => 'Jim',
    'last_name' => 'Brennan',
  ),
  '6ce98c71-80c8-8b04-1767-52ccdd1b7c96' => 
  array (
    'id' => '6ce98c71-80c8-8b04-1767-52ccdd1b7c96',
    'user_name' => 'TestUser',
    'first_name' => 'John',
    'last_name' => 'Doe',
  ),
  'seed_max_id' => 
  array (
    'id' => 'seed_max_id',
    'user_name' => 'max',
    'first_name' => 'Max',
    'last_name' => 'Jensen',
  ),
  '1d77045b-fb16-d925-b19e-52c85d82bf81' => 
  array (
    'id' => '1d77045b-fb16-d925-b19e-52c85d82bf81',
    'user_name' => 'PortalUser',
    'first_name' => 'Portal',
    'last_name' => 'User',
  ),
  'seed_sally_id' => 
  array (
    'id' => 'seed_sally_id',
    'user_name' => 'sally',
    'first_name' => 'Sally',
    'last_name' => 'Bronsen',
  ),
  'seed_sarah_id' => 
  array (
    'id' => 'seed_sarah_id',
    'user_name' => 'sarah',
    'first_name' => 'Sarah',
    'last_name' => 'Smith',
  ),
  '95803cf3-84ea-493a-a030-52b0abcd9b0c' => 
  array (
    'id' => '95803cf3-84ea-493a-a030-52b0abcd9b0c',
    'user_name' => 'test',
    'first_name' => 'test',
    'last_name' => 'test',
  ),
  'seed_will_id' => 
  array (
    'id' => 'seed_will_id',
    'user_name' => 'will',
    'first_name' => 'Will',
    'last_name' => 'Westin',
  ),
);

PHP Array Returned from my Database result of Subscribed Users...

$subscriberIdFromDb = array (
  0 => '1d77045b-fb16-d925-b19e-52c85d82bf81',
  1 => '95803cf3-84ea-493a-a030-52b0abcd9b0c',
  2 => 'seed_max_id',
  3 => 'seed_sally_id',
);





echo '<pre>';
print_r($subscriberIdFromDb);
echo '</pre>';


$hiddenSubscriberListIds = '';

$selHtml = '<select id="subscribersSelection" name="subscribers" data-placeholder="" style="width:350px;" multiple tabindex="3">';
foreach ($userArray as $userIdKey => $value) {

    if(in_array($userIdKey, $subscriberIdFromDb)) {
        echo '$userIdKey = '.$userIdKey. '<br>';
        $selHtml .= '<option value="'.$userIdKey.'" selected>'.$value['user_name'].'</option>';
        $hiddenSubscriberListIds .= '<input type="hidden" name="subscribers[]" value="'.$userIdKey.'" id="'.$userIdKey.'">';
    }else{
        $selHtml .= '<option value="'.$userIdKey.'">'.$value['user_name'].'</option>';
    }
}
$selHtml .= '</select>';
$selHtml .= $hiddenSubscriberListIds;

echo $selHtml;

?>

When the above code is ran, it outputs this HTML...

<select id="subscribersSelection" name="subscribers" data-placeholder="" style="width:350px;" multiple tabindex="3">
    <option value="seed_chris_id">chris</option>
    <option value="1" selected>jasondavis</option>
    <option value="1702c3d0-df12-2d1b-d964-521becb5e3ad">Jeff</option>
    <option value="seed_jim_id">jim</option>
    <option value="6ce98c71-80c8-8b04-1767-52ccdd1b7c96">TestUser</option>
    <option value="seed_max_id" selected>max</option>
    <option value="1d77045b-fb16-d925-b19e-52c85d82bf81" selected>PortalUser</option>
    <option value="seed_sally_id" selected>sally</option>
    <option value="seed_sarah_id">sarah</option>
    <option value="95803cf3-84ea-493a-a030-52b0abcd9b0c" selected>test</option>
    <option value="seed_will_id">will</option>
</select>
<input type="hidden" name="subscribers[]" value="1" id="1">
<input type="hidden" name="subscribers[]" value="seed_max_id" id="seed_max_id">
<input type="hidden" name="subscribers[]" value="1d77045b-fb16-d925-b19e-52c85d82bf81" id="1d77045b-fb16-d925-b19e-52c85d82bf81">
<input type="hidden" name="subscribers[]" value="seed_sally_id" id="seed_sally_id">
<input type="hidden" name="subscribers[]" value="95803cf3-84ea-493a-a030-52b0abcd9b0c" id="95803cf3-84ea-493a-a030-52b0abcd9b0c">

You can see <option value="1" selected>jasondavis</option> and <input type="hidden" name="subscribers[]" value="1" id="1"> get added even though that ID 1 does not exist in my Array!!!

Try to tell in_array to use "strict" mode to compare the values.

in_array($userIdKey, $subscriberIdFromDb, TRUE)

What is happening is: $userIdKey is 1 (an integer) and it's being compared to every value in $subscriberIdFromDb. It's comparing like this: 1 == '1d77045b-fb16-d925-b19e-52c85d82bf81'.

The two types don't match, therefore PHP converts the 2nd to an int. When converting a string to an int, PHP reads the string until the 1st non-digit character. Thus 1 == (int)'1d77045b-fb16-d925-b19e-52c85d82bf81' or 1 == 1. (see: https://eval.in/197010)

Passing TRUE as a 3rd parameter to in_array makes it use === instead. 1 === '1d77045b-fb16-d925-b19e-52c85d82bf81' is false since the types don't match.

P.S. You could also fix this by changing 1 => to '1' => in $userArray.