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条)

报告相同问题?