PDA

View Full Version : Searching an array



Joe!
04-02-2010, 01:36 PM
What would be the easiest way to search an array for a unique value, and then telling me what array key it is in. I've tried array_search but that doesn't seem to work, I think it's because i'm searching for a string, not exactly sure though. Each key of the array is structured like this

"objectno";"objectname";"objectclassname";"objecttype";"description";
I'm trying to search the whole thing for the objectno, and then tell me which key it is :)
cheers for any help

BoyBetterKnow
05-02-2010, 10:31 AM
errr i dnt get u but er


in_array()

?

Joe!
05-02-2010, 11:06 AM
http://uk.php.net/manual/en/function.array-search.php

Well that function does exactly what I want it to do, but I cant get it to work :(

Delatory
06-02-2010, 08:48 AM
$search="2";
$array=array("1;1name;1objectclass;1objecttype;1description;", "2;2name;2objectclass;2objecttype;2description;", "2;2name;2objectclass;2objecttype;2description;");
$found=0;
for($i=0; $i<count($array); $i++){
if(preg_match("/".$search.";(.+?);(.+?);(.+?);(.+?);/", $array[$i])){
$found=$i;
}
}
echo $found;

Want to hide these adverts? Register an account for free!