У меня есть следующий массив:
$inboxMessages = array( 105775 => array( //index is thread_id 0 => array( 'id' => 85, 'thread_id' => 105775, 'message' => "hello", 'created_at' => "May 20, 2015", 'datetime' => 1432118191, 'sender_id' => 13, ), 1 => array( 'id' => 70, 'thread_id' => 105775, 'message' => "hii", 'created_at' => "May 19, 2015", 'datetime' => 1432021227, 'sender_id' => 13, ) ), 224199 => array( //index is thread_id 0 => array( 'id' => 88, 'thread_id' => 224199, 'message' => "yessss...", 'created_at' => "May 20, 2015", 'datetime' => 1432306513, 'sender_id' => 14, ), 1 => array( //index is thread_id 'id' => 75, 'thread_id' => 224199, 'message' => "hellowwww...", 'created_at' => "May 19, 2015", 'datetime' => 1432021227, 'sender_id' => 14, ) ), 107917 => array( //index is thread_id 0 => array( 'id' => 56, 'thread_id' => 107917, 'message' => "how ru??", 'created_at' => "May 16, 2015", 'datetime' => 1431792155, 'sender_id' => 14, ), 1 => array( 'id' => 30, 'thread_id' => 107917, 'message' => "hi.. im fine.", 'created_at' => "May 6, 2015", 'datetime' => 1430920006, 'sender_id' => 14, ), 2 => array( 'id' => 30, 'thread_id' => 107917, 'message' => "hi!!!!..how ru??", 'created_at' => "May 6, 2015", 'datetime' => 1430920006, 'sender_id' => 14, ) ), 378552 => array( //index is thread_id 0 => array( 'id' => 108, 'thread_id' => 378552, 'message' => "hi", 'created_at' => "May 29, 2015", 'datetime' => 1432906923, 'sender_id' => 14, ), 1 => array( 'id' => 107, 'thread_id' => 378552, 'message' => "hi.", 'created_at' => "May 29, 2015", 'datetime' => 1432903194, 'sender_id' => 14, ) ) ); Итак, мне нужен вывод следующим образом:
 $inboxMessages = array( 378552 => array( //index is thread_id 0 => array( 'id' => 108, 'thread_id' => 378552, 'message' => "hi", 'created_at' => "May 29, 2015", 'datetime' => 1432906923, 'sender_id' => 14, ), 1 => array( 'id' => 107, 'thread_id' => 378552, 'message' => "hi.", 'created_at' => "May 29, 2015", 'datetime' => 1432903194, 'sender_id' => 14, ) ), 224199 => array( //index is thread_id 0 => array( 'id' => 88, 'thread_id' => 224199, 'message' => "yessss...", 'created_at' => "May 20, 2015", 'datetime' => 1432306513, 'sender_id' => 14, ), 1 => array( //index is thread_id 'id' => 75, 'thread_id' => 224199, 'message' => "hellowwww...", 'created_at' => "May 19, 2015", 'datetime' => 1432021227, 'sender_id' => 14, ) ), 105775 => array( //index is thread_id 0 => array( 'id' => 85, 'thread_id' => 105775, 'message' => "hello", 'created_at' => "May 20, 2015", 'datetime' => 1432118191, 'sender_id' => 13, ), 1 => array( 'id' => 70, 'thread_id' => 105775, 'message' => "hii", 'created_at' => "May 19, 2015", 'datetime' => 1432021227, 'sender_id' => 13, ) ), 107917 => array( //index is thread_id 0 => array( 'id' => 56, 'thread_id' => 107917, 'message' => "how ru??", 'created_at' => "May 16, 2015", 'datetime' => 1431792155, 'sender_id' => 14, ), 1 => array( 'id' => 30, 'thread_id' => 107917, 'message' => "hi.. im fine.", 'created_at' => "May 6, 2015", 'datetime' => 1430920006, 'sender_id' => 14, ), 2 => array( 'id' => 30, 'thread_id' => 107917, 'message' => "hi!!!!..how ru??", 'created_at' => "May 6, 2015", 'datetime' => 1430920006, 'sender_id' => 14, ) ), ); 
  Я хочу отсортировать основной массив по значению массива datetime для thread_id . 
  В массиве thread_id уже выполняется сортировка. 
  Так что я просто хочу отсортировать основной массив thread_id . 
Мне нужно, чтобы последнее время datetime находилось сверху на массиве
Я пробовал до сих пор И его работы для меня:
 function comp($a, $b) { if ($a[0]->datetime == $b[0]->datetime) { return 0; } return $a[0]->datetime > $b[0]->datetime ? -1 : 1; } uasort($inboxMessages, "comp"); 
  А также попробуйте с id сообщения: 
 function comp($a, $b) { if ($a[0]->id == $b[0]->id) { return 0; } return $a[0]->id > $b[0]->id ? -1 : 1; } uasort($inboxMessages, "comp"); 
       
		  Вы можете использовать uasort() следующим образом, чтобы получить следующий формат 
 function comp($a, $b) { if ($a[0]['datetime'] == $b[0]['datetime']) { return 0; } return $a[0]['datetime'] > $b[0]['datetime'] ? -1 : 1; } uasort($inboxMessages, "comp"); 
Проверить демоверсию
  Использовать uasort 
 function cmp($a, $b) { if ($a[0]['datetime'] == $b[0]['datetime']) { return 0; } return ($a[0]['datetime'] < $b[0]['datetime']) ? 1 : -1; } uasort($inboxMessages, "cmp"); 
В зависимости от версии PHP вы можете указать «cmp» как анонимную функцию. Также имейте в виду, что этот метод основан на том факте, что каждый поток имеет хотя бы одно сообщение.