1

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!!!

2
  • 1
    i dont understand why you dont loop through $subscriberIdFromDb rather than $userArray Commented Sep 22, 2014 at 22:33
  • @Dagon The reason for looping through $userArray is because I need to build the Selection list with ALL users in the list, the other array is simply to have some of them pre-selected Commented Sep 22, 2014 at 22:45

1 Answer 1

6

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.

Sign up to request clarification or add additional context in comments.

4 Comments

var_dump(in_array(1,array('sdfsdf'))); returns false
@FuzzyTree: Well yeah, (int)'sdfsdf' is 0. Try var_dump(in_array(1,array('1sdfsdf')));. When converting a string to an int, PHP reads the string until the 1st non-digit character.
I was just doing a bunch of test and discovered this to be the problem, what I ended up doing was instead of using the Array Key in my comparison, I used the value as the Int in the value is wrapped in '' but I like your idea and explanation as to why it was doing it, thanks!
@jasondavis: Glad I could be of help. Another solution might actually be: in_array("$userIdKey", $subscriberIdFromDb). Basically, just converting the $userIdKey into a string before comparing.

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.