我写了这样一个数组:
$tags = [
"Channel1\n" . 26210,
"Channel2\n" . 7896 . "\n---------------",
"Channel3\n" . 5035 . "\n---------------",
"Channel4\n" . 25794 . "\n---------------",
"Channel5\n" . 91143 . "\n---------------",
"Channel6\n" . 42075 . "\n---------------",
"Channel7\n" . 11815 . "\n---------------",
"Channel8\n" . 180 . "\n---------------",
"Channel9\n" . 171 . "\n---------------",
"Channel10\n" . 82106 . "\n---------------"
];
现在我使用sort()
函数对这个数组进行排序,但我需要它只在两个字符串(数组的特定部分)之间显示的那些整数排序。例如:Channel9 - 171 , Channel8 - 180 ...
答案 0 :(得分:1)
我可能会在数组中得到数字并对其进行排序:
$nums = preg_filter('/[^\n][^\d]+/', '', $tags); //or preg_replace()
array_multisort($nums, $tags);
稍微长一点的方法:
usort($tags, function($a, $b) {
$a = explode("\n", $a)[1];
$b = explode("\n", $b)[1];
if ($a == $b) { return 0; }
return ($a < $b) ? -1 : 1;
});