【发布时间】:2013-06-06 10:08:22
【问题描述】:
我正在尝试通过使用 uasort 和正则表达式将数组“$refs”与字符串“$term”进行比较来对数组“$refs”进行排序:
这是我的数组:
Array
(
[0] => Array
(
[id] => 71063
[uniqid] => A12171063
[label] => Pratique...
)
[1] => Array
(
[id] => 71067
[uniqid] => A12171067
[label] => Etre....
)
...
和我的代码:
uasort($refs, function ($a, $b) use ($term) {
$patern='/^' . $term . '/';
if ((preg_match($patern, $a['label']) - preg_match($patern, $b['label']) )== 0) {
return 0;
}
if ((preg_match($patern, $a['label']) - preg_match($patern, $b['label'])) == 1) {
return -1;
}
if ((preg_match($patern, $a['label']) - preg_match($patern, $b['label'])) == -1) {
return 1;
}
});
我只有 0 个喜欢的回报,我的错误在哪里!:/ 谢谢
【问题讨论】:
-
$refs = $a = $b = $term = ??? -
$refs 是数组,$term 是要比较的词
-
这就像 perl 中的 1-liner。如果有帮助,我可以在 Perl 中写一些东西
-
是的,为什么不呢,我以它为例。谢谢
-
工作正常here。我想知道
$term中有什么?如果它包含正则表达式元字符,您可能需要preg_quote它。