Jump to content

Need help looping a loop (with for or foreach)


snowman2344

Recommended Posts

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
Share on other sites

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
Share on other sites

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
Share on other sites

$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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.