snowman2344 Posted November 17, 2007 Share Posted November 17, 2007 Need help finding a Fire Truck in a PHP array. I need help finding a Fire Truck’s assigned number in the following array. The trucks assignment is P331. I need to find that in the array and find what variable it is in. The array is generated by the Fire Department Dispatch system and I cannot change the structure of the array. The array changes every 5 min and the truck that I am looking for can be anywhere in the array it can be in a string separated by commas or by it self or not in the array at all. Here is an example The Array Array ( [0] => BELSIZE DR, TT [1] => THURSTON RD / CLEVELAND ST [2] => 2007-11-07 09:19:21 [3] => F07126861 [4] => Medical - VSA [5] => 1 [6] => 321 [7] => R321, [8] => KINGSTON RD, SC [9] => MARKHAM RD / BRINLOOR BLVD [10] => 2007-11-07 09:47:07 [11] => F07126865 [12] => Medical - Seizures/Convulsions [13] => 1 [14] => 223 [15] => P223, [16] => BATHURST ST, TT [17] => STEWART ST / KING ST W [18] => 2007-11-07 09:49:24 [19] => F07126866 [20] => Alarm Commercial/Industrial [21] => 1 [22] => 331 [23] => R426, P331, P332, A315, C34, [24] => EAST WILLOW GT, SC [25] => RAVINE PARK CRES / EAST AVE [26] => 2007-11-07 09:52:25 [27] => F07126867 [28] => Check Call Assist [29] => 1 [30] => 215 [31] => A215, [32] => HIGH PARK AVE, TT [33] => BLOOR ST W / GLENLAKE AVE [34] => 2007-11-07 09:53:11 [35] => F07126868 [36] => Alarm Highrise Residential [37] => 1 [38] => 423 [39] => R423, P424, A423, C42, [40] => BABY POINT CRES, YK [41] => L'ESTRANGE PL / BABY POINT RD [42] => 2007-11-07 09:55:56 [43] => F07126870 [44] => Alarm Residential [45] => 1 [46] => 422 [47] => P422, P431, A433, C44, ) $search_what = 'P331'; //This is what I need to find What I need out of this is the variable it is in [23] => R426, P331, P332, A315, C34, $key = 23 // from some function beyond my programming skill I tried the following but it will not find P331 in the array because it is within the string separated by commas. It will however find A215 without problem because it is not in a string [31] => A215, The code <?php $search_what = 'P331'; $key = array_search($search_what.',', $abc); if (empty($key)){ echo $search_what; echo " is not on a call right now"; }else{ echo $search_what; echo " Is key number "; echo $key; } ?> Thanks in advance. Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted November 17, 2007 Share Posted November 17, 2007 Try something like this: <?php $str = 'P331'; $ftf = false; $ft_key = -1; for ($i=0;$i<count($your_array) && !$ftf;$i++) $tmp = explode(',',$your_array[$i]; if (in_array($str,$tmp)) { $ftf = true; $ft_key = $i; } } if ($ftf) echo 'Fire Truck ' . $str . ' found at key ' . $ft_key; else echo 'Fire Truck ' . $str . ' is not on call now'; ?> Note: code not tested. Ken Quote Link to comment Share on other sites More sharing options...
wsantos Posted November 17, 2007 Share Posted November 17, 2007 Ken let me modify your loop, assuming the data is 8 columns wide. for ($i=0;$i<count($your_array) && $ft_key=-1;$i+=7) { $tmp = explode(',',$your_array[$i]); if (in_array($str,$tmp)) $ft_key = $i; } Quote Link to comment Share on other sites More sharing options...
rarebit Posted November 18, 2007 Share Posted November 18, 2007 This may be off but... $what = "P331"; print "<h2>EXAMPLE 1</h2>"; $test = array(); $test[] = "R426, P331, P332, A315, C34,"; $test[] = "P331, P332, A315, C34,"; $test[] = "R426, P332, A315, C34, P331";"; $test[] = P331; $test[] = "R426, P332, A315, C34, PP331"; $test[] = "R426, P3311, P332, A315, C34,"; $test[] = "P3313, P332, A315, C34,"; foreach($test as $e) { //if(preg_match("/((^".$what."$)|(^".$what.",)|( ".$what.",)|( ".$what."$))/", $test)) if(preg_match("/(((^)|( ))".$what."((,)|($)))/", $e)) { print "Correct <br>\n"; } else { print "Incorrect <br>\n"; } } print "\n<br><br>\n"; print "<h2>EXAMPLE 2</h2>"; $test = array(); $test[] = "BELSIZE DR, TT"; $test[] = "THURSTON RD / CLEVELAND ST"; $test[] = "2007-11-07 09:19:21"; $test[] = "F07126861"; $test[] = "Medical - VSA"; $test[] = 1; $test[] = 321; $test[] = "R321,"; $test[] = "KINGSTON RD, SC"; $test[] = "MARKHAM RD / BRINLOOR BLVD"; $test[] = "2007-11-07 09:47:07"; $test[] = "F07126865"; $test[] = "Medical - Seizures/Convulsions"; $test[] = 1; $test[] = 223; $test[] = "P223,"; $test[] = "BATHURST ST, TT"; $test[] = "STEWART ST / KING ST W"; $test[] = "2007-11-07 09:49:24"; $test[] = "F07126866"; $test[] = "Alarm Commercial/Industrial"; $test[] = 1; $test[] = 331; $test[] = "R426, P331, P332, A315, C34,"; $test[] = "EAST WILLOW GT, SC"; $test[] = "RAVINE PARK CRES / EAST AVE"; $test[] = "2007-11-07 09:52:25"; $test[] = "F07126867"; $test[] = "Check Call Assist"; $test[] = 1; $test[] = 215; $test[] = "A215,"; $test[] = "HIGH PARK AVE, TT"; $test[] = "BLOOR ST W / GLENLAKE AVE"; $test[] = "2007-11-07 09:53:11"; $test[] = "F07126868"; $test[] = "Alarm Highrise Residential"; $test[] = 1; $test[] = 423; $test[] = "R423, P424, A423, C42,"; $test[] = "BABY POINT CRES, YK"; $test[] = "L'ESTRANGE PL / BABY POINT RD"; $test[] = "2007-11-07 09:55:56"; $test[] = "F07126870"; $test[] = "Alarm Residential"; $test[] = 1; $test[] = 422; $test[] = "P422, P431, A433, C44,"; foreach($test as $e) { if(preg_match("/(((^)|( ))".$what."((,)|($)))/", $e)) { print "Correct <br>\n"; } else { print "Incorrect <br>\n"; } } Oh, you'll want to mix it with one of the other examples here... Quote Link to comment Share on other sites More sharing options...
snowman2344 Posted November 18, 2007 Author Share Posted November 18, 2007 rarebit looks like your script finds the truck but what I need out of this is the variable it is in e.g. [23] => R426, P331, P332, A315, C34, $key = 23 //would be the output of the script Thanks for your input kenrbnsn i could not get your for loop to loop the output of $ft_key = -1 and not the correct key Thanks for your input Quote Link to comment Share on other sites More sharing options...
snowman2344 Posted November 18, 2007 Author Share Posted November 18, 2007 kenrbnsn Got the loop to work (just some small syntax errors got it though ) But now there is a new problem it will find a truck name in the string but not by it self or if it is at the end of the string. Turns out there is a space after the comma and it is not on the lines with just one truck or after the last one. E.G. [7] => R321, // there is no space after the comma [23] => R426, P331, P332, A315, C34, //there is a space after the commas except the last one $str = 'C34'; $ftf = false; $ft_key = -1; for ($i=0;$i<count($your_array) && !$ftf;$i++) { $tmp = explode(', ',$your_array[$i]); if (in_array($str,$tmp)) { $ftf = true; $ft_key = $i; } } if ($ftf) echo 'Fire Truck ' . $str . ' found at key ' . $ft_key . '<br>'; else echo 'Fire Truck ' . $str . ' is not on call now (FT_key → ' . $ft_key .'#### i → '. $i . ')<br>'; Quote Link to comment Share on other sites More sharing options...
snowman2344 Posted November 18, 2007 Author Share Posted November 18, 2007 i have a solution don't know if it is pretty but it works for ($i=0;$i<count($your_array) && !$ftf;$i++) { $tmp = explode(', ',$your_array[$i]); $tmp1 = preg_replace ( "/\s\s+/" , " " , $tmp ); $tmp2 = preg_replace ( "/,/" , "" , $tmp1 ); if (in_array($str,$tmp2)) { $ftf = true; $ft_key = $i; } } Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.