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
aceconcepts
DevNet Resident
Posts: 1424 Joined: Mon Feb 06, 2006 11:26 am
Location: London
Post
by aceconcepts » Thu Jan 17, 2008 8:32 am
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?
VladSun
DevNet Master
Posts: 4313 Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria
Post
by VladSun » Thu Jan 17, 2008 8:49 am
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
aceconcepts
DevNet Resident
Posts: 1424 Joined: Mon Feb 06, 2006 11:26 am
Location: London
Post
by aceconcepts » Thu Jan 17, 2008 8:58 am
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?
VladSun
DevNet Master
Posts: 4313 Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria
Post
by VladSun » Thu Jan 17, 2008 9:02 am
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
Zoxive
Forum Regular
Posts: 974 Joined: Fri Apr 01, 2005 4:37 pm
Location: Bay City, Michigan
Post
by Zoxive » Thu Jan 17, 2008 9:11 am
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
aceconcepts
DevNet Resident
Posts: 1424 Joined: Mon Feb 06, 2006 11:26 am
Location: London
Post
by aceconcepts » Thu Jan 17, 2008 9:34 am
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?
Zoxive
Forum Regular
Posts: 974 Joined: Fri Apr 01, 2005 4:37 pm
Location: Bay City, Michigan
Post
by Zoxive » Thu Jan 17, 2008 9:58 am
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.
aceconcepts
DevNet Resident
Posts: 1424 Joined: Mon Feb 06, 2006 11:26 am
Location: London
Post
by aceconcepts » Thu Jan 17, 2008 10:06 am
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.
Zoxive
Forum Regular
Posts: 974 Joined: Fri Apr 01, 2005 4:37 pm
Location: Bay City, Michigan
Post
by Zoxive » Thu Jan 17, 2008 10:31 am
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.
aceconcepts
DevNet Resident
Posts: 1424 Joined: Mon Feb 06, 2006 11:26 am
Location: London
Post
by aceconcepts » Thu Jan 17, 2008 10:37 am
Hi Zoxive,
I have found the problem. The script relies on a url variable wich is set locally i.e.
. 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
Cheers