in_array - exact match

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

in_array - exact match

Post 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?
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: in_array - exact match

Post by VladSun »

Code: Select all

 
$wsArray = Array('value_11', 'value_2');
 
if(in_array('value_1', $wsArray))
    echo 1;
 
No echo ...
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: in_array - exact match

Post 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?
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: in_array - exact match

Post by VladSun »

Hm... try to set it to strict mode:

Code: Select all

if(in_array("value_1", $wsArray, true))
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
Zoxive
Forum Regular
Posts: 974
Joined: Fri Apr 01, 2005 4:37 pm
Location: Bay City, Michigan

Re: in_array - exact match

Post 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
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: in_array - exact match

Post 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?
User avatar
Zoxive
Forum Regular
Posts: 974
Joined: Fri Apr 01, 2005 4:37 pm
Location: Bay City, Michigan

Re: in_array - exact match

Post 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.
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: in_array - exact match

Post 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.
User avatar
Zoxive
Forum Regular
Posts: 974
Joined: Fri Apr 01, 2005 4:37 pm
Location: Bay City, Michigan

Re: in_array - exact match

Post 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.
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: in_array - exact match

Post 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
Post Reply