这是我当前的数组
Array
(
[0] => [10,13],
[1] => [11,15],
[2] => [12,7],
[3] => [1,0],
[4] => [2,0],
[5] => [3,0],
[6] => [4,0],
[7] => [5,0],
[8] => [6,0],
[9] => [7,0],
[10] => [8,0],
[11] => [9,0]
);
我想按值(逗号前的值)对此数组进行排序 结果应该是这样的:
Array
(
[1] => [1,0],
[2] => [2,0],
[3] => [3,0],
[4] => [4,0],
[5] => [5,0],
[6] => [6,0],
[7] => [7,0],
[8] => [8,0],
[9] => [9,0],
[10] => [10,13],
[11] => [11,15],
[12] => [12,7]
);
是否可以在PHP中使用?
答案 0 :(得分:4)
选项1:如果二维阵列尝试这一个
$data = array();
$data[0] = array(10, 13);
$data[2] = array(11, 15);
$data[3] = array(12, 7);
$data[4] = array(1, 0);
$data[5] = array(2, 0);
$data[6] = array(3, 0);
$data[7] = array(4, 0);
$data[8] = array(5, 0);
$data[9] = array(6, 0);
$data[10] = array(7, 0);
$data[11] = array(8, 0);
$data[12] = array(9, 0);
echo "<pre>";
//print_r($data);
array_multisort($data);
print_r($data);
echo "</pre>";
?>
输出
Array
(
[0] => Array
(
[0] => 1
[1] => 0
)
[1] => Array
(
[0] => 2
[1] => 0
)
[2] => Array
(
[0] => 3
[1] => 0
)
[3] => Array
(
[0] => 4
[1] => 0
)
[4] => Array
(
[0] => 5
[1] => 0
)
[5] => Array
(
[0] => 6
[1] => 0
)
[6] => Array
(
[0] => 7
[1] => 0
)
[7] => Array
(
[0] => 8
[1] => 0
)
[8] => Array
(
[0] => 9
[1] => 0
)
[9] => Array
(
[0] => 10
[1] => 13
)
[10] => Array
(
[0] => 11
[1] => 15
)
[11] => Array
(
[0] => 12
[1] => 7
)
)
选项2:如果一维阵列尝试这一个
$data = array();
$data[0] = "10,13";
$data[2] = "11,15";
$data[3] = "12,7";
$data[4] = "1,0";
$data[5] = "2,0";
$data[6] = "3,0";
$data[7] = "4,0";
$data[8] = "5,0";
$data[9] = "6,0";
$data[10] = "7,0";
$data[11] = "8,0";
$data[12] = "9,0";
echo "<pre>";
//print_r($data);
echo "</pre>";
function dataSort($data){
$data_temp =array();
foreach($data as $item){
$data_temp[] = explode(",",$item);
}
array_multisort($data_temp);
$data =array();
foreach($data_temp as $item){
$data[] = $item[0].",".$item[1];
}
return $data;
}
$data = dataSort($data);
print_r($data);
输出
Array
(
[0] => 1,0
[1] => 2,0
[2] => 3,0
[3] => 4,0
[4] => 5,0
[5] => 6,0
[6] => 7,0
[7] => 8,0
[8] => 9,0
[9] => 10,13
[10] => 11,15
[11] => 12,7
)
答案 1 :(得分:2)
试试这个
function mSort($a,$b) {
if ($a[0] == $b[0]) {
return 0;
}
return ($a[0] < $b[0]) ? -1 : 1;
}
usort($arr, "mSort");
print_r($arr);
答案 2 :(得分:2)
你可以使用usort()函数或任何this list,你附近有什么。例如:
<?php
$array = array (
array(10,13),
array(11,15),
array(12,7),
array(1,0),
array(2,0),
array(3,0),
array(4,0),
array(5,0),
array(6,0),
array(7,0),
array(8,0),
array(9,0)
);
function mySort( $l, $r ) {
return (( $l[0] == $r[0] ) ? 0 : ($l[0] > $r[0] ? 1 : -1) );
}
usort( $array, 'mySort' );
print_r( $array );
?>
答案 3 :(得分:1)
这就是你想要的DEMO
<?php
$yourArray = array(
1 => array(
10,
13
),
2 => array(
11,
15
),
3 => array(
12,
7
),
4 => array(
1,
0
),
5 => array(
2,
0
),
6 => array(
3,
0
),
7 => array(
4,
0
),
8 => array(
5,
0
),
9 => array(
6,
0
),
10 => array(
7,
0
),
11 => array(
8,
0
),
12 => array(
9,
0
)
);
$array = array();
foreach ($yourArray as $key => $row) {
$array[$key] = $yourArray['value'];
}
array_multisort($array, SORT_DESC, $yourArray);
$yourArray = array_combine(range(1, count($yourArray)), array_values($yourArray));
print_r($yourArray);
?>
答案 4 :(得分:1)
你可以尝试
$new = array();
foreach($first_array as $key => $value) {
$new[$key] = $value;
}
array_multisort($new, SORT_ASC, $first_array);
答案 5 :(得分:0)
您需要访问$value
作为对$array
中的条目的参考:
private function orderByKey(&$array) {
ksort($array);
foreach($array as &$value) {
if (is_array($value)) {
$this->orderByKey($value);
}
}
}
答案 6 :(得分:0)
您可以对此
使用array_multisort()函数$arr = array([10,13],[11,15],[12,7],[1,0],[2,0],[3,0],[4,0],[5,0],[6,0],[7,0],[8,0],[9,0]);
array_multisort($arr);
var_dump($arr);exit;
答案 7 :(得分:0)
由于您正在处理的值是字符串(根据您对我的评论的回复),您需要隔离第一个数值,然后根据它进行排序。 (请注意,这不支持相同“第一个数字”的倍数。)
示例:
$a = array("[1,10]", "[2,19]", "[6,13]", "[2,15]", "[4,16]");
$sortable = array();
foreach($a as $subject) {
$start = strpos($subject, "[");
$end = strpos($subject, ",");
$ndx = substr($subject, $start, $end - $start);
$sortable[$ndx] = $subject;
}
ksort($sortable);
var_dump($sortable);
var_dump
将产生:
array(4) {
["[1"]=>
string(6) "[1,10]"
["[2"]=>
string(6) "[2,15]"
["[4"]=>
string(6) "[4,16]"
["[6"]=>
string(6) "[6,13]"
}
答案 8 :(得分:0)
你需要使用usort功能:
$a = array([10,13],[11,15],[12,7],[1,0],[2,0],[3,0],[4,0],[5,0],[6,0],[7,0],[8,0],[9,0]);
print_r($a);
usort($a, "cmp");
print_r($a);
?>
请参阅此处的示例:http://ideone.com/gv4k70