|
|
|
ทำอย่างไรครับ ต้องการเรียงลำดับข้อมูล Array sort น้อยไปมาก |
|
|
|
|
|
|
|
Code (PHP)
<?php
$process = array(
'1' => array('arrival' => 1, 'service' => 3,),
'2' => array('arrival' => 2, 'service' => 6,),
'3' => array('arrival' => 0, 'service' => 4,),
'4' => array('arrival' => 0, 'service' => 2,),
'5' => array('arrival' => 8, 'service' => 5,),
);
foreach ($process as $key => $row) {
$arrival[$key] = $row['arrival'];
}
array_multisort($arrival, SORT_ASC, $process);
print_r($process);
?>
ผลลัพธ์เรียงจากน้อยไปมาก
Array (
[0] => Array ( [arrival] => 0 [service] => 2 )
[1] => Array ( [arrival] => 0 [service] => 4 )
[2] => Array ( [arrival] => 1 [service] => 3 )
[3] => Array ( [arrival] => 2 [service] => 6 )
[4] => Array ( [arrival] => 8 [service] => 5 )
)
ค่า [arrival] มีเลข 0 เหมือนกัน 2 ตัว ผมไม่เข้าใจทำไม Array มันไม่เรียง $process[3] ก่อน $process[4] ครับ(จากโค้ด)
ต้องเขียนโค้ดยังไงถึงจะเรียงลำดับผลลัพธ์เป็นแบบนี้ครับ
[0] => Array ( [arrival] => 0 [service] => 4 )
[1] => Array ( [arrival] => 0 [service] => 2 )
[2] => Array ( [arrival] => 1 [service] => 3 )
[3] => Array ( [arrival] => 2 [service] => 6 )
[4] => Array ( [arrival] => 8 [service] => 5 )
Tag : PHP
|
ประวัติการแก้ไข 2014-11-04 22:01:45 2014-11-04 22:04:05 2014-11-04 22:25:49
|
|
|
|
|
Date :
2014-11-04 21:58:08 |
By :
demoproject |
View :
2286 |
Reply :
6 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ใช sort ครับ
Code (PHP)
$fruits = array("lemon", "orange", "banana", "apple");
sort($fruits);
foreach ($fruits as $key => $val) {
echo "fruits[" . $key . "] = " . $val . "\n";}
|
|
|
|
|
Date :
2014-11-05 13:46:38 |
By :
bunchuai |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
อยากรู้เหมือนกันครับ
|
|
|
|
|
Date :
2014-11-07 21:39:31 |
By :
HIPOP |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ช่วยหน่อยนะครับ :((
|
|
|
|
|
Date :
2014-11-10 00:59:07 |
By :
Atom |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Code (PHP)
function array_sort_col( &$source_array, $column, $type = SORT_ASC) {
$buff_sort = array();
foreach ($source_array as $key=> $row) {
$buff_sort[$key] = $row[$column];
}
array_multisort($buff_sort, $dir, $source_array);
}
$array = array(
array( 1, 2, 3),
array( 10, 11, 12),
array( 7, 8, 9),
array( 4, 5, 6)
)
array_sort_col($array, 2);
echo '<pre>' ,print_r($array, true) , '</pre>';
|
ประวัติการแก้ไข 2014-11-10 12:08:59 2014-11-10 12:09:28
|
|
|
|
Date :
2014-11-10 12:07:52 |
By :
Chaidhanan |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Code (PHP)
<?php
foreach ($process as $key => $row) {
$process[$key] = array();
array_push($process[$key],array('arrival'=>$row[arrival],'key'=>$key,'service'=>$row[service]));
}
array_multisort($process, SORT_ASC);
print_r($process);
?>
|
|
|
|
|
Date :
2014-11-10 12:08:36 |
By :
gaowteen |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 03
|