douhui1333 2011-12-25 10:02
浏览 36
已采纳

PHP array_unique和rsort问题

I am using preg_match_all to pull phone numbers from a thread. This puts them into an array, im applying both rsort and array_unique to the matches variable, however they have no effect what so ever... The array_unique would eliminate matches that only come up from a quote or response duplicate, and the rsort should make the last index the first, the second to last index, second, etc...

preg_match_all('~0-9]{3}-[0-9]{3}-[0-9]{4}~', $data, $matches) 
$result = array_unique($matches);
rsort($result);
var_dump($result);

Output:

array
0 => 
array
  0 => string '111-111-1111' (length=12)
  1 => string '222-222-2222' (length=12)
  2 => string '333-333-3333' (length=12)
  3 => string '444-444-4444' (length=12)
  4 => string '555-555-5555' (length=12)
  5 => string '555-555-5555' (length=12)
  6 => string '555-555-5555' (length=12)

Needs to be:

array
0 => 
array
  0 => string '555-555-5555' (length=12)
  1 => string '444-444-4444' (length=12)
  2 => string '333-333-3333' (length=12)
  3 => string '222-222-2222' (length=12)
  4 => string '111-111-1111' (length=12)
  • 写回答

2条回答 默认 最新

  • douyao6842 2011-12-25 10:11
    关注

    I think you need the first element in the matches array.

    preg_match_all('~0-9]{3}-[0-9]{3}-[0-9]{4}~', $data, $matches) 
    $aList = $matches[0];
    $result = array_unique($aList);
    
    rsort($result);
    var_dump($result);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面