|
|
|
PHP Sort Array สอบถามเรื่องเรียงลำดับตัวอักษรภาษาไทยใน Array ครับ รบกวนด้วยครับ |
|
|
|
|
|
|
|
Code (PHP)
<?php
$fruits = array("lemon", "orange", "banana", "apple");
sort($fruits);
foreach ($fruits as $key => $val) {
echo "fruits[" . $key . "] = " . $val . "\n";
}
?>
|
|
|
|
|
Date :
2011-06-07 10:38:34 |
By :
webmaster |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
อันนี้แบบจัดเรียงได้ตามต้องการ
Code (PHP)
<?php
function array_sort($array, $on, $order=SORT_ASC)
{
$new_array = array();
$sortable_array = array();
if (count($array) > 0) {
foreach ($array as $k => $v) {
if (is_array($v)) {
foreach ($v as $k2 => $v2) {
if ($k2 == $on) {
$sortable_array[$k] = $v2;
}
}
} else {
$sortable_array[$k] = $v;
}
}
switch ($order) {
case SORT_ASC:
asort($sortable_array);
break;
case SORT_DESC:
arsort($sortable_array);
break;
}
foreach ($sortable_array as $k => $v) {
$new_array[$k] = $array[$k];
}
}
return $new_array;
}
$people = array(
12345 => array(
'id' => 12345,
'first_name' => 'Joe',
'surname' => 'Bloggs',
'age' => 23,
'sex' => 'm'
),
12346 => array(
'id' => 12346,
'first_name' => 'Adam',
'surname' => 'Smith',
'age' => 18,
'sex' => 'm'
),
12347 => array(
'id' => 12347,
'first_name' => 'Amy',
'surname' => 'Jones',
'age' => 21,
'sex' => 'f'
)
);
print_r(array_sort($people, 'age', SORT_DESC)); // Sort by oldest first
print_r(array_sort($people, 'surname', SORT_ASC)); // Sort by surname
/*
Array
(
[12345] => Array
(
[id] => 12345
[first_name] => Joe
[surname] => Bloggs
[age] => 23
[sex] => m
)
[12347] => Array
(
[id] => 12347
[first_name] => Amy
[surname] => Jones
[age] => 21
[sex] => f
)
[12346] => Array
(
[id] => 12346
[first_name] => Adam
[surname] => Smith
[age] => 18
[sex] => m
)
)
Array
(
[12345] => Array
(
[id] => 12345
[first_name] => Joe
[surname] => Bloggs
[age] => 23
[sex] => m
)
[12347] => Array
(
[id] => 12347
[first_name] => Amy
[surname] => Jones
[age] => 21
[sex] => f
)
[12346] => Array
(
[id] => 12346
[first_name] => Adam
[surname] => Smith
[age] => 18
[sex] => m
)
)
*/
?>
|
|
|
|
|
Date :
2011-06-07 10:39:26 |
By :
webmaster |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ขอบคุณครับพี่ win ขอลองก่อนนะครับ
|
|
|
|
|
Date :
2011-06-07 10:49:30 |
By :
rattakorn |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
work มากเลยครับพี่ขอบคุณครับ
|
|
|
|
|
Date :
2011-06-07 11:01:45 |
By :
rattakorn |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
มีอีกหลายแบบครับ การ sort ข้อมูลจากเว็บ php.net
http://php.net/manual/en/function.sort.php
|
|
|
|
|
Date :
2011-06-08 09:47:21 |
By :
ideaza |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 01
|