Jump to content

foreach loop, return results when true, or else keep looping


poe

Recommended Posts

i am trying to make a counter that shows the tally and where people come from and since i do get alot of people coming from mail i end up with a lot of things like:

 

2 -> b3.mail.yahoo.com 

1 -> b5.mail.yahoo.com 

1 -> bl149w.blu149.mail.live.com 

2 -> by105w.bay105.mail.live.com 

3 -> by108w.bay108.mail.live.com 

2 -> by125w.bay125.mail.live.com 

1 -> co102w.col102.mail.live.com 

 

as you can see, b3.mail.yahoo.com and b5.mail.yahoo.com are both from mail.yahoo.com

so the desired result i want is

 

3 -> mail.yahoo.com

9 -> mail.live.com

 

 

here is what i have so far, but i dont think it is right

basically i pass the 'host' to the function

it checks my array list of emails

if it finds one, it uses the 'cleaner' version, or else it stays the same.

 

at the moment i think my unction loos through each array, but it returns the result on the first loop.

i guess i need it to loop through, and if it finds something in the atrray, then return otherwise, keep looping, until it runs out, and at that point: return

 

function clean_email($chkhost)
{

$arrEmail = array('mail.live.com', 'mail.yahoo.com');

foreach($arrEmail as $k => $cleanEmail)
{

$haystack = strtolower($chkhost);
$needle = $cleanEmail;

$pos = strpos($haystack, $needle);

$result = ($pos === false) ? $chkhost : $cleanEmail;

return $result;
}
}

you have return $result; inside the foreach so yes it will exit on first, you need to move it to other side for foreach end }

 

also

 

since you are not placing indexes in your array you don't need the => part in the foreach

so

foreach($arrEmail as $k => $cleanEmail)

should be

foreach($arrEmail as $cleanEmail)

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.