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 Link to comment https://forums.phpfreaks.com/topic/77934-need-help-looping-a-loop-with-for-or-foreach/ 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 />'; } Link to comment https://forums.phpfreaks.com/topic/77934-need-help-looping-a-loop-with-for-or-foreach/#findComment-394479 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... Link to comment https://forums.phpfreaks.com/topic/77934-need-help-looping-a-loop-with-for-or-foreach/#findComment-394480 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 Link to comment https://forums.phpfreaks.com/topic/77934-need-help-looping-a-loop-with-for-or-foreach/#findComment-394484 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... Link to comment https://forums.phpfreaks.com/topic/77934-need-help-looping-a-loop-with-for-or-foreach/#findComment-394497 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>"; } } Link to comment https://forums.phpfreaks.com/topic/77934-need-help-looping-a-loop-with-for-or-foreach/#findComment-394500 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] Link to comment https://forums.phpfreaks.com/topic/77934-need-help-looping-a-loop-with-for-or-foreach/#findComment-394509 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 Link to comment https://forums.phpfreaks.com/topic/77934-need-help-looping-a-loop-with-for-or-foreach/#findComment-394658 Share on other sites More sharing options...
snowman2344 Posted November 20, 2007 Author Share Posted November 20, 2007 any ideas ??? Link to comment https://forums.phpfreaks.com/topic/77934-need-help-looping-a-loop-with-for-or-foreach/#findComment-394797 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; } } Link to comment https://forums.phpfreaks.com/topic/77934-need-help-looping-a-loop-with-for-or-foreach/#findComment-394854 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. ??? ??? Link to comment https://forums.phpfreaks.com/topic/77934-need-help-looping-a-loop-with-for-or-foreach/#findComment-396762 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++) { Link to comment https://forums.phpfreaks.com/topic/77934-need-help-looping-a-loop-with-for-or-foreach/#findComment-396886 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.