snowman2344 Posted November 19, 2007 Share Posted November 19, 2007 I have the following “for loop” I am looking for a single value in an array. It works fine Once. I want to run this loop for several values. How can I loop the loop every time returning the text output result with the next value in the array? This is the origanal working (Once) code. $str = 'C34'; $ft_key = -1; for ($i=0;$i<count($your_array) && !$ftf;$i++) { $ft_key = -1; $tmp = explode(', ',$your_array[$i]);//brake apart truck string $tmp1 = preg_replace ( "/\s\s+/" , " " , $tmp );//remove white spaces $tmp2 = preg_replace ( "/,/" , "" , $tmp1 );//remove comma if (in_array($str,$tmp2)) { $ftf = true; $ft_key = $i; } } if ($ftf){ echo "<br>"; echo $str; echo " Is on Sceen at → ";echo $abc[$ft_key-7]; }else{ echo $str; echo " is not on a call right now";echo "<br>";echo "<br>"; } What i want to do i look (loop) for $str = array('C34','P123','R421'); and return the rusult for each Quote Link to comment Share on other sites More sharing options...
mjahkoh Posted November 19, 2007 Share Posted November 19, 2007 Not exactly an expert in php but this should take u home Assuming the array is called $products then for ( $row = 0; $row < 3; $row++ ) { for ( $column = 0; $column < 3; $column++ ) { echo '|'.$products[$row][$column]; } echo '|<br />'; } Quote Link to comment Share on other sites More sharing options...
ToonMariner Posted November 19, 2007 Share Posted November 19, 2007 why don't you use array_keys instead? it will be much faster... Quote Link to comment Share on other sites More sharing options...
snowman2344 Posted November 19, 2007 Author Share Posted November 19, 2007 Thanks for your input but i am not using mysql for this application Quote Link to comment Share on other sites More sharing options...
ToonMariner Posted November 19, 2007 Share Posted November 19, 2007 array_keys() is a php function - it returns the keys of an array whose element value is that which you are searching for.. the only 'looping' you will have to do is each value you are looking for... Quote Link to comment Share on other sites More sharing options...
snowman2344 Posted November 19, 2007 Author Share Posted November 19, 2007 i tryed the following but all the output values are the same (it puts in the first loop values for all) i need to know where to put somting like unset so it can pick up the new loop It sucks not being a guru. $str = array('HZ332','P123','R421'); $ftf = false; $ft_key = -1; for ($truck=0;$truck<count($str);$truck++){ for ($i=0;$i<count($your_array) && !$ftf;$i++) { $tmp = explode(', ',$your_array[$i]);//brake apart truck string $tmp1 = preg_replace ( "/\s\s+/" , " " , $tmp );//remove white spaces $tmp2 = preg_replace ( "/,/" , "" , $tmp1 );//remove comma if (in_array($str[$truck],$tmp2)) { $ftf = true; $ft_key = $i; } } if ($ftf){ echo "<br>"; echo $str[$truck]; echo " Is on Sceen at → ";echo $abc[$ft_key-7]; }else{ echo "<br>";echo $str[$truck]; echo " is not on a call right now";echo "<br>";echo "<br>"; } } Quote Link to comment Share on other sites More sharing options...
ToonMariner Posted November 19, 2007 Share Posted November 19, 2007 there are so many ways of achieving this - the best one would be to use something like... <?php $str = array('HZ332','P123','R421'); $matches = array_intersect($yourarray, $str); print_r($matches); ?>[/url] Quote Link to comment Share on other sites More sharing options...
snowman2344 Posted November 19, 2007 Author Share Posted November 19, 2007 I cannot compare the two arrays because the first array’s strings first need to be taken apart (that is what the code dose that I have). If one of you gurus has a minute to look at the following code let me know why the values repeat. It loops the correct amount of times but gives me the values for to first value for all of them. I think I need a “UNSET” somewhere maybe?? $str = array('HZ332','P123','R421'); $ftf = false; $ft_key = -1; for ($truck=0;$truck<count($str);$truck++){ for ($i=0;$i<count($your_array) && !$ftf;$i++) { $tmp = explode(', ',$your_array[$i]);//brake apart truck string $tmp1 = preg_replace ( "/\s\s+/" , " " , $tmp );//remove white spaces $tmp2 = preg_replace ( "/,/" , "" , $tmp1 );//remove comma if (in_array($str[$truck],$tmp2)) { $ftf = true; $ft_key = $i; } } if ($ftf){ echo "<br>"; echo $str[$truck]; echo " Is on Sceen at → ";echo $abc[$ft_key-7]; }else{ echo "<br>";echo $str[$truck]; echo " is not on a call right now";echo "<br>";echo "<br>"; } } Thanks Quote Link to comment Share on other sites More sharing options...
snowman2344 Posted November 20, 2007 Author Share Posted November 20, 2007 any ideas ??? Quote Link to comment Share on other sites More sharing options...
snowman2344 Posted November 20, 2007 Author Share Posted November 20, 2007 $str = array('R421','A421','AL421','P421B','P421CR'); foreach ($str as $s){ unset ($ft_key, $i, $tmp); $ftf = false; $ft_key = -1; for ($i=0;$i<count($abc) && !$ftf;$i++) { $tmp = explode(', ',$abc[$i]);//brake apart truck string $tmp1 = preg_replace ( "/\s\s+/" , " " , $tmp );//remove white spaces $tmp2 = preg_replace ( "/,/" , "" , $tmp1 );//remove comma if (in_array($s,$tmp2)) { $ftf = true; $ft_key = $i; } } Quote Link to comment Share on other sites More sharing options...
snowman2344 Posted November 22, 2007 Author Share Posted November 22, 2007 This code runs slow any idea how to speed it up. Takes on average 4 sec to run. ??? ??? Quote Link to comment Share on other sites More sharing options...
ToonMariner Posted November 22, 2007 Share Posted November 22, 2007 don't evaluate count on each loop!!! $stopat = count($abc); for ($i=0;$i<$stopat && !$ftf;$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.