php in_array - unexpected behavior

Not sure why this is happening, but my script doesn't seem to be able to return true for in_array more than once...

I'm posting the code below:


$saved = '15,22';    
$set = explode(",",$saved); //results in Array ( [0] => 15 [1] => 22 ) 

//Then, I query the database:

$result = pg_query("SELECT did,vid,iid,value FROM demographicValues");
if(pg_num_rows($result) > 0) {
  while($r = pg_fetch_array($result)) {
     $demo[$r['did']][$r['vid']]['value'] = $r['value'];
     if(in_array($r['vid'], $set)) {
       $demo[$r['did']][$r['vid']]['status'] = 1;
     }
  }
} else...

/*
If I print_r $demo, you can see that the vid 22 is in there, so, I'm not understanding why the status isn't being set accordingly?
*/
    Array
    (
        [Mant] => Array
            (
                [15] => Array
                    (
                        [value] => Proper
                        [checked] => 1
                    )

                [16] => Array
                    (
                        [value] => Parish
                    )
        [Comp] => Array
            (
                [22] => Array
                    (
                        [value] => 65 - 70
                    )

                [23] => Array
                    (
                        [value] => 35 - 50
                    )
        )

)

Open in new window

n00b0101Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Beverley PortlockCommented:
Your array as shown in print_r layout does not have a key for "vid", just for "Mant" and "Comp" so it looks to me like in_array is correct
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Avinash ZalaWeb ExpertCommented:
Yes, bportlock that's what i am thinking since long..

Thanks
Addy
0
Shinesh PremrajanEngineering ManagerCommented:
Actually there are two problems

The array that you are finally getting is due to the

 $demo[$r['did']][$r['vid']]['value'] = $r['value'];
If its entering the in_array condition then you should get the status also in the array, which is not there in the one you outputted.
 $demo[$r['did']][$r['vid']]['status'] = 1;

ok so try this

 if(array_search($r['vid'], $set))

instead if in_array()

Hope this helps
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Beverley PortlockCommented:
@Addy... I was scratching my head my self.

@N00b0101 - are you expecting in_array to check the sub-arrays as well? In general PHP array functions only check the top leve of the array that you give them. If you want them to inspect sub-arrays then you need to consider looking at the array_walk function that applies a function to every member and sub-member of an array

http://uk.php.net/manual/en/function.array-walk-recursive.php
0
hieloCommented:
You can try adding false as the third parameter:
if( in_array($r['vid'], $set,false) )

BUT if $set is large, the in_array may turn out to be slow. My suggestion would be to flip the $set array, so that the values become keys and then simply use isset():
$saved = '15,22';    
$set = array_flip(explode(",",$saved)); //results in Array ( [15] => 0 [22] =>1 ) 

//Then, I query the database:

$result = pg_query("SELECT did,vid,iid,value FROM demographicValues");
if(pg_num_rows($result) > 0) {
  while($r = pg_fetch_array($result,NULL,PGSQL_ASSOC)) {
     $demo[$r['did']][$r['vid']]['value'] = $r['value'];
     if( isset($set[ $r['vid'] ]) ) {
       $demo[$r['did']][$r['vid']]['status'] = 1;
     }
  }
} else...

Open in new window

0
n00b0101Author Commented:
I'll accept this one as a solution, because my array was indeed, incorrect...  

When I did a var_dump instead of print_r, I saw that in my $set array, there was an extra space around the value.  So, I fixed that and was able to get it to work... Really appreciate everyone's input...  
0
hieloCommented:
Well this is a surprising "solution".
>>Your array as shown in print_r layout does not have a key for "vid", just for "Mant" and "Comp" so it looks to me like in_array is correct

The commented code clearly states:
If I print_r $demo

but the statement is:
if(in_array($r['vid'], $set))

Notice that it is searching in $set, NOT in $demo! So of course it will not find it.

However, from the posted result of print_r, $['vid'] seems to be 22, which does exist in $set!!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.