PHP array_unique 和 rsort 问题
我正在使用 preg_match_all 从线程中提取电话号码。这将它们放入一个数组中,我将 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);
输出:
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)
需要是:
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 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)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为您需要 matches 数组中的第一个元素。
I think you need the first element in the matches array.
preg_match_all 给出一个二维数组。你需要有 $matches 的第一个元素。用 unique 和 rsort 进一步处理它。
preg_match_all gives a two dimensional array. you need to have the first element of $matches. to further process it with unique and rsort.