Page 1 of 1

in_array - exact match

Posted: Thu Jan 17, 2008 8:32 am
by aceconcepts
Hi,

I am trying to find out whether a value exists in an array:

Code: Select all

 
if(in_array('value_1', $wsArray))
{
    //do something                              
}
 
The above code does not just return 'value_1' matches it also returns matches such as 'value_2'.

How can I find out if an exact value is in an array?

Re: in_array - exact match

Posted: Thu Jan 17, 2008 8:49 am
by VladSun

Code: Select all

 
$wsArray = Array('value_11', 'value_2');
 
if(in_array('value_1', $wsArray))
    echo 1;
 
No echo ...

Re: in_array - exact match

Posted: Thu Jan 17, 2008 8:58 am
by aceconcepts
I'm not sure if it makes a difference but this is all the code used:

Code: Select all

 
$wsArray=unserialize($rowRes['arrWorkshops']);
            
    if(in_array("value_1", $wsArray))
    {
        $valueExists=1;                 
    } 
 
I am getting the array from a database table which has been serialized.

The above example by VladSun ultimately the same as what I have - I do not use echo anywhere.

Any ideas?

Re: in_array - exact match

Posted: Thu Jan 17, 2008 9:02 am
by VladSun
Hm... try to set it to strict mode:

Code: Select all

if(in_array("value_1", $wsArray, true))

Re: in_array - exact match

Posted: Thu Jan 17, 2008 9:11 am
by Zoxive
aceconcepts wrote:I'm not sure if it makes a difference but this is all the code used:

Code: Select all

 
$wsArray=unserialize($rowRes['arrWorkshops']);
            
    if(in_array("value_1", $wsArray))
    {
        $valueExists=1;                 
    } 
 
I am getting the array from a database table which has been serialized.

The above example by VladSun ultimately the same as what I have - I do not use echo anywhere.

Any ideas?
var_dump $wsArray, and make sure it is spelled Exactly the same. in_array is case sensative, and should not be partially matching.

If you are still having problems, can you give us the exact array that is having problems, and what you are trying to do.

Code: Select all

$Array = array('value_2','value_3','value_11');
 
if(in_array('value_1',$Array)){
  echo 'Was in Array';
}else{
  echo 'Was not in Array';
}
// Was not in Array

Re: in_array - exact match

Posted: Thu Jan 17, 2008 9:34 am
by aceconcepts
It's still not working even in strict mode.

Here is an array I am having problems with:

Code: Select all

 
Array ( [0] => osgp_1 [1] => fibromyalgia_1 [2] => gout_2 [3] => wp_2 [4] => pp_1 [5] => sb_1 [6] => cs_1 )
 
What I am trying to do is display all records where the value "gout_1" exists inside the array shown above. Currently when I run the code I previously posted "gout_2" shows up as well as "gout_1" in other arrays. To me this suggests that partial matches are being returned - but they shouldn't should they?

Can you notice a mistake?

Re: in_array - exact match

Posted: Thu Jan 17, 2008 9:58 am
by Zoxive
Its probably something with your code.

I'm guessing the Variable is not being reset after the loop. So when it gets to 'gout_2' the Variable you are checking is set, because it was set last time.

But lets see some code.

Re: in_array - exact match

Posted: Thu Jan 17, 2008 10:06 am
by aceconcepts
The code is pretty lengthy however i do reset the variable on each loop.

Another thing is that I am testing the array with values that I know for sure do not exist like:

Code: Select all

 
$wsArray=array("value_1", "value_2")
 
Using in_array to check these values, the entire recordset still displays when it shouldnt.

Re: in_array - exact match

Posted: Thu Jan 17, 2008 10:31 am
by Zoxive
We can't help without more code.

Unless you can show us the Exact Array you are using with the Exact way you are using in_array to match that array. And the output/what it is doing wrong.

in_array does not Partial match and is case-sensitive. With the strict setting, it matches strictly by Types: ie. Integers.

Re: in_array - exact match

Posted: Thu Jan 17, 2008 10:37 am
by aceconcepts
Hi Zoxive,

I have found the problem. The script relies on a url variable wich is set locally i.e.

Code: Select all

$id=$_REQUEST['id'];
. After the first loop the local variable is reset to a different value by default. This is correct for my script i just setup the logic incorrectly.

Thank you so much for all of everyone's help - I have certainly learn't more about in_array :D

Cheers