0

i have a csv file containing telephone numbers.

There is about 15 numbers with the area code +44 and 20 numbers beginning with +64 (there are about 40 area codes). i need to echo one number of each set and drop the remains.

i loaded all the content using fgetcsv(). but i cant find a way to do the filtering part. can some please give me an idea how to sort this out in php?

1
  • What about area codes that have one or three letters? Commented Jun 9, 2011 at 12:59

3 Answers 3

1

Try this replace $numbers with your csv file content.

$numbers = array('+1134124', '+11412421', '+41125125','+41125124','+41125144','+41155124','+44125124','+44155124');                          

$final = array();

$received = array();

foreach($numbers as $snKey => $snValue )
{

    $snCode = substr($snValue,0,3);

    if(in_array($snCode,$received))
        continue;
    else
    {
        $received[] = $snCode;
        $final[] = $snValue;
    }
}
print_r($final);
Sign up to request clarification or add additional context in comments.

Comments

1
$numbers = array('+1134124', '+11412421', '+41125125', …);

$filtered = array_reduce($numbers, function ($f, $num) {
    return $f + array(substr($num, 0, 3) => $num);
}, array());

Comments

0

you can filter them while read line from the file

like:

 $array = array(); 
    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
            $prefix = substr($data, 0, 3);
            if(in_array($prefix, $array))
               continue;
            else{
               $array[] = prefix;
               # save the line or echo
            } 

     }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.