|
|
|
| Есть массив
Array
(
[0] => Array
(
[ID] => 256
[ParentID] => 101
[IP] => 127.0.0.1
[CommentID] => 2
[Comment] => Тест1
[newtime] => 1219251652
)
[1] => Array
(
[ID] => 256
[ParentID] => 101
[IP] => 127.0.0.1
[CommentID] => 1
[Comment] => Тест1
[newtime] => 1219251640
)
[2] => Array
(
[ID] => 256
[ParentID] => 101
[IP] => 127.0.0.1
[CommentID] => 2
[Comment] => Тест1
[newtime] => 1219251585
)
[3] => Array
(
[ID] => 256
[ParentID] => 101
[IP] => 127.0.0.1
[CommentID] => 4
[Comment] => Тест1
[newtime] => 1219251094
)
[4] => Array
(
[ID] => 256
[ParentID] => 101
[IP] => 127.0.0.1
[CommentID] => 6
[Comment] => Тест1
[newtime] => 1219250990
)
[5] => Array
(
[ID] => 248
[ParentID] => 101
[IP] => 127.0.0.1
[CommentID] => 8
[Comment] => Новый Тест
[newtime] => 1219251585
)
[6] => Array
(
[ID] => 248
[ParentID] => 101
[IP] => 127.0.0.1
[CommentID] => 3
[Comment] => Новый Тест
[newtime] => 1219251103
)
[7] => Array
(
[ID] => 248
[ParentID] => 101
[IP] => 127.0.0.1
[CommentID] => 7
[Comment] => Новый Тест
[newtime] => 1219250990
)
[8] => Array
(
[ID] => 167
[ParentID] => 101
[IP] => 127.0.0.1
[CommentID] => 3
[Comment] => Новый х3
[newtime] => 1219251859
)
[9] => Array
(
[ID] => 209
[ParentID] => 248
[IP] => 127.0.0.1
[CommentID] => 4
[Comment] => Без теста
[newtime] => 1220726671
)
)
|
Возможно, ли произвести сортировку этого массива по его ключам ParentID, ID и newtime?
Чтобы получился вот такой массив:
Array
(
[8] => Array
(
[ID] => 167
[ParentID] => 101
[IP] => 127.0.0.1
[CommentID] => 3
[Comment] => Новый х3
[newtime] => 1219251859
)
[7] => Array
(
[ID] => 248
[ParentID] => 101
[IP] => 127.0.0.1
[CommentID] => 7
[Comment] => Новый Тест
[newtime] => 1219250990
)
[6] => Array
(
[ID] => 248
[ParentID] => 101
[IP] => 127.0.0.1
[CommentID] => 3
[Comment] => Новый Тест
[newtime] => 1219251103
)
[5] => Array
(
[ID] => 248
[ParentID] => 101
[IP] => 127.0.0.1
[CommentID] => 8
[Comment] => Новый Тест
[newtime] => 1219251585
)
[4] => Array
(
[ID] => 256
[ParentID] => 101
[IP] => 127.0.0.1
[CommentID] => 6
[Comment] => Тест1
[newtime] => 1219250990
)
[3] => Array
(
[ID] => 256
[ParentID] => 101
[IP] => 127.0.0.1
[CommentID] => 4
[Comment] => Тест1
[newtime] => 1219251094
)
[2] => Array
(
[ID] => 256
[ParentID] => 101
[IP] => 127.0.0.1
[CommentID] => 2
[Comment] => Тест1
[newtime] => 1219251585
)
[1] => Array
(
[ID] => 256
[ParentID] => 101
[IP] => 127.0.0.1
[CommentID] => 1
[Comment] => Тест1
[newtime] => 1219251640
)
[0] => Array
(
[ID] => 256
[ParentID] => 101
[IP] => 127.0.0.1
[CommentID] => 2
[Comment] => Тест1
[newtime] => 1219251652
)
[9] => Array
(
[ID] => 209
[ParentID] => 248
[IP] => 127.0.0.1
[CommentID] => 4
[Comment] => Без теста
[newtime] => 1220726671
)
)
|
| |
|
|
|
|
|
|
|
для: smit272
(08.09.2008 в 15:05)
| | usort() | |
|
|
|
|
|
|
|
для: Trianon
(08.09.2008 в 16:35)
| | а функцию для usort? | |
|
|
|
|
|
|
|
для: smit272
(08.09.2008 в 16:36)
| | придется написать, само собой. | |
|
|
|
|
|
|
|
для: Trianon
(08.09.2008 в 16:42)
| | ))
так по идее в этом то и был вопрос.
потому что не получаеться написать нормальную функцию :) | |
|
|
|
|
|
|
|
для: smit272
(08.09.2008 в 17:37)
| | пользовательскую функцию сравнения по одному ключу получается написать?
показывайте то что получается. | |
|
|
|
|
|
|
|
для: Trianon
(08.09.2008 в 17:39)
| |
<?
function compare($a, $b) {
return strcmp($a["ParentID"], $b["ParentID"]); //выбираем нужный ключ для сортировки
}
usort($tree, "compare");
?>
|
| |
|
|
|
|
|
|
|
для: smit272
(08.09.2008 в 17:58)
| |
<?
function compare($a, $b)
{
if($a["ParentID"] != $b["ParentID"])
return 2 * ($a["ParentID"] > $b["ParentID"]) - 1;
if($a["newtime"] != $b["newtime"])
return 2 * ($a["newtime"] > $b["newtime"]) - 1;
if($a["ID"] != $b["ID"])
return 2 * ($a["ID"] > $b["ID"]) - 1;
return 0;
}
|
Но следует учесть что если данные числовые, то и элементы массива должны быть числовыми . А не текстовыми. Так что вероятно потребуется приведение к int при сравнении. | |
|
|
|
|
|
|
|
для: smit272
(08.09.2008 в 17:37)
| | Например так:
<?
function so ($a, $b) {
if ( $a['ParentID'] > $b['ParentID']) return 1;
if ( $a['ParentID'] < $b['ParentID']) return 0;
if ( $a['ParentID'] == $b['ParentID']) return -1;
}
uasort($arr, 'so');
?>
|
| |
|
|
|
|
|
|
|
для: Temnovit
(08.09.2008 в 17:42)
| | а если на 2 и на 3? | |
|
|
|
|
|
|
|
для: smit272
(08.09.2008 в 17:49)
| | >а если на 2 и на 3?
Не понимаю вопроса... | |
|
|
|
|
|
|
|
для: Temnovit
(08.09.2008 в 18:03)
| | Ну надо для 3-ех различных ключей
ParentID, ID, newtime | |
|
|
|
|
|
|
|
для: Temnovit
(08.09.2008 в 17:42)
| | > if ( $a['ParentID'] < $b['ParentID']) return 0;
> if ( $a['ParentID'] == $b['ParentID']) return -1;
издеваемся? | |
|
|
|
|
|
|
|
для: Trianon
(08.09.2008 в 18:02)
| | Путаемся :) Наоборот, конечно. | |
|
|
|
|
|
|
|
для: Temnovit
(08.09.2008 в 18:03)
| | всем спасибо.
все работает | |
|
|
|