Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Some in_array logic help please?

Status
Not open for further replies.

BobMCT

IS-IT--Management
Sep 11, 2000
756
US
I've been playing with this for way too long. So I thought I would ask here.

I'm trying to check if a array element value can be found as a data element in a reference array. I'm using this:
if (!in_array($arg, $arrayname, true)) {
this would occur if the $arg value is NOT found in the array
}

What I'm experiencing is even if the searched arrayname contains a substring of the $argument is returning true.
Example:

If I search for abc and the array element contains abcdef it would return found.

What do I need to do to assure the element I'm testing for is the exact same match as the argument value?
example:

If the argument was abc then the array element value must be exactly abc


Any hint, suggestions greatly appreciated.

Thanks
 
Hi

Are you sure ? [tt]in_array()[/tt] works out of the box exactly as you described :
Code:
Interactive mode enabled

[blue]php >[/blue] var_dump(in_array('abc', ['abcdef']));
bool(false)

[blue]php >[/blue] var_dump(in_array('abcdefghi', ['abcdef']));
bool(false)

[blue]php >[/blue] var_dump(in_array('abcDEF', ['abcdef']));
bool(false)

[blue]php >[/blue] var_dump(in_array('abcdef', ['abcdef']));
bool(true)

Could you post some exact input data samples for which it fails ?


Feherke.
feherke.ga
 
Well it turns out that the test was operating properly all along. I added the "true" option and now it does not come back true with a "contained within" match. Also, I was improperly removing my argument which gave the impression of a true when it was not. This instruction was not one that I often used. Takes time to understand how it works.

Thanks for the thoughts.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top