[英]PHP array_unique and rsort issues
I am using preg_match_all to pull phone numbers from a thread. 我正在使用preg_match_all从线程中提取电话号码。 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...
这将它们放入一个数组中,我将rsort和array_unique同时应用于matches变量,但是它们没有任何效果...... array_unique将消除仅来自引用或响应副本的匹配,并且rsort应该使最后一个索引是第一个,倒数第二个索引,第二个索引......
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)
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);
preg_match_all gives a two dimensional array. preg_match_all给出一个二维数组。 you need to have the first element of $matches.
你需要拥有$ matches的第一个元素。 to further process it with unique and rsort.
用unique和rsort进一步处理它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.