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

Jas*_*vis 1 php arrays

下面我有PHP代码...
- 迭代用户数组
- 构建所有用户的HTML选择列表
- 如果$subscriberIdFromDb数组中的用户存在于所有用户的数组中,则它会selected向HTML Selection元素添加一个属性.
- 满足上述条件,它还添加了一个带有用户ID的隐藏表单字段元素.

现在在$subscriberIdFromDb下面的数组中,您可以看到4个测试用户.但是,当我运行下面的代码时,似乎总是添加第5个ID为1的用户.

我无法弄清楚为什么第五个用户被添加,我将我的数据库结果导出到这些测试数组并尝试在另一个系统上它仍然这样做,所以它似乎是这个代码中的某些东西导致了问题.

是什么原因造成了这个问题?

PHP数组从测试系统中所有用户的数据库结果返回...

<?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',
  ),
);
Run Code Online (Sandbox Code Playgroud)

PHP数组从我的数据库结果返回订阅用户...

$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;

?>
Run Code Online (Sandbox Code Playgroud)

运行上面的代码时,它会输出这个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">
Run Code Online (Sandbox Code Playgroud)

即使我的数组中不存在ID 1,您也可以看到<option value="1" selected>jasondavis</option><input type="hidden" name="subscribers[]" value="1" id="1">添加!

Roc*_*mat 5

尝试告诉in_array使用"严格"模式来比较值.

in_array($userIdKey, $subscriberIdFromDb, TRUE)
Run Code Online (Sandbox Code Playgroud)

发生的事情是:$userIdKey1(整数)并且它与每个值进行比较$subscriberIdFromDb.这是比较:1 == '1d77045b-fb16-d925-b19e-52c85d82bf81'.

这两种类型不匹配,因此PHP将第二个转换为int.将字符串转换为int时,PHP会读取字符串直到第1个非数字字符.因此1 == (int)'1d77045b-fb16-d925-b19e-52c85d82bf81'1 == 1.(见:https://eval.in/197010)

传递TRUE作为一个第三个参数in_array使得它使用===来代替. 1 === '1d77045b-fb16-d925-b19e-52c85d82bf81'由于类型不匹配,因此为false.

PS您也可以通过更改1 =>为来解决此'1' =>问题$userArray.