[英]compare and filter values in array in php
i have a csv file containing telephone numbers.我有一个包含电话号码的 csv 文件。
There is about 15 numbers with the area code +44 and 20 numbers beginning with +64 (there are about 40 area codes).大约有 15 个区号 +44 的号码和 20 个以 +64 开头的号码(大约有 40 个区号)。 i need to echo one number of each set and drop the remains.
我需要回显每组中的一个数字并丢弃剩余部分。
i loaded all the content using fgetcsv().我使用 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?
有人可以告诉我如何在 php 中解决这个问题吗?
$numbers = array('+1134124', '+11412421', '+41125125', …);
$filtered = array_reduce($numbers, function ($f, $num) {
return $f + array(substr($num, 0, 3) => $num);
}, array());
Try this replace $numbers with your csv file content.尝试将 $numbers 替换为 csv 文件内容。
$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);
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
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.