Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> Sortowanie tablicy po polu z datą
khalka
post 13.05.2019, 13:36:50
Post #1





Grupa: Zarejestrowani
Postów: 6
Pomógł: 0
Dołączył: 15.05.2018

Ostrzeżenie: (0%)
-----


Witam,

Mam problem którego nie mogę przeskoczyć.

Mamy tablice

  1. (
  2. [0] => Array
  3. (
  4. [field_5c6fe44e60cb8] => 2019-05-13 13:22:15
  5. [field_5c6fe4ab60cb9] =>
  6. [field_5c6fe4fd60cba] => Kamil
  7. [field_5c6fe51260cbb] => 3333
  8. )
  9.  
  10. [1] => Array
  11. (
  12. [field_5c6fe44e60cb8] => 2019-05-13 13:22:26
  13. [field_5c6fe4ab60cb9] =>
  14. [field_5c6fe4fd60cba] => Kamil
  15. [field_5c6fe51260cbb] => 22222
  16. )
  17.  
  18. [2] => Array
  19. (
  20. [field_5c6fe44e60cb8] => 2019-05-13 13:24:46
  21. [field_5c6fe4ab60cb9] =>
  22. [field_5c6fe4fd60cba] => Kamil
  23. [field_5c6fe51260cbb] => 1111
  24. )
  25.  
  26. [3] => Array
  27. (
  28. [field_5c6fe44e60cb8] => 2019-05-13 13:25:18
  29. [field_5c6fe4ab60cb9] =>
  30. [field_5c6fe4fd60cba] => Kamil
  31. [field_5c6fe51260cbb] => 3333
  32. )
  33.  
  34. [4] => Array
  35. (
  36. [field_5c6fe44e60cb8] => 2019-05-13 13:31:19
  37. [field_5c6fe4ab60cb9] =>
  38. [field_5c6fe4fd60cba] => Kamil
  39. [field_5c6fe51260cbb] => 5555
  40. )
  41.  
  42. )


I potrzebuję posortować ją po polu z datą "field_5c6fe44e60cb8" - najnowszy element na górze.

Czyli np klucz "4" powinien być w tym wypadku "0" i całą zawartością być na górze.

Może ktoś pomóc jak to zrobić?
Go to the top of the page
+Quote Post
Pyton_000
post 13.05.2019, 13:57:29
Post #2





Grupa: Zarejestrowani
Postów: 8 068
Pomógł: 1414
Dołączył: 26.10.2005

Ostrzeżenie: (0%)
-----


https://www.php.net/manual/en/function.array-multisort.php Example #3

I do tego jeszcze komentarz: https://www.php.net/manual/en/function.arra...sort.php#119343

Ten post edytował Pyton_000 13.05.2019, 13:58:16
Go to the top of the page
+Quote Post
khalka
post 13.05.2019, 16:49:59
Post #3





Grupa: Zarejestrowani
Postów: 6
Pomógł: 0
Dołączył: 15.05.2018

Ostrzeżenie: (0%)
-----


Nie bardzo wiem jak to użyć przy moim schemacie tablicy.
Go to the top of the page
+Quote Post
adbacz
post 13.05.2019, 19:30:26
Post #4





Grupa: Zarejestrowani
Postów: 532
Pomógł: 24
Dołączył: 15.04.2011
Skąd: Kalisz

Ostrzeżenie: (0%)
-----


Wiesz, że jak poświęcisz godzinę na spróbowanie to nauczysz się więcej niż z gotowca, który ktoś Ci poda? smile.gif

W komentarzu w manualu jest użyta zmienna $data, to jest Twoja tablica z danymi. A zamiast 'id' w indeksie 'field' w tablicy $sortings daj nazwę Twojego pola, po których chcesz sortować.
Go to the top of the page
+Quote Post
khalka
post 13.05.2019, 20:17:41
Post #5





Grupa: Zarejestrowani
Postów: 6
Pomógł: 0
Dołączył: 15.05.2018

Ostrzeżenie: (0%)
-----


Dlatego długo odpisuje bo ciągle próbuje rozwiązać problem smile.gif

  1. $value = array(
  2. '0' => array(
  3. 'field_5c6fe44e60cb8' => '2019-05-13 11:22:15',
  4. 'field_5c6fe4ab60cb9' => '',
  5. 'field_5c6fe4fd60cba' => 'Kamil',
  6. 'field_5c6fe51260cbb' => '22',
  7. ),
  8. '1' => array(
  9. 'field_5c6fe44e60cb8' => '2019-04-13 22:22:15',
  10. 'field_5c6fe4ab60cb9' => '',
  11. 'field_5c6fe4fd60cba' => 'Kamil',
  12. 'field_5c6fe51260cbb' => '333',
  13. ),
  14. '2' => array(
  15. 'field_5c6fe44e60cb8' => '2019-02-13 13:22:15',
  16. 'field_5c6fe4ab60cb9' => '',
  17. 'field_5c6fe4fd60cba' => 'Kamil',
  18. 'field_5c6fe51260cbb' => '444',
  19. ),
  20. '3' => array(
  21. 'field_5c6fe44e60cb8' => '2019-07-13 12:22:15',
  22. 'field_5c6fe4ab60cb9' => '',
  23. 'field_5c6fe4fd60cba' => 'Kamil',
  24. 'field_5c6fe51260cbb' => '111',
  25. ),
  26.  
  27.  
  28.  
  29. );
  30.  
  31. echo '<pre>';
  32. print_R($value);
  33. echo '</pre>';
  34. echo '=========';
  35.  
  36.  
  37. $sortings = [
  38. [
  39. 'field' => 'field_5c6fe44e60cb8',
  40. 'direction' => SORT_DESC,
  41. ],
  42. ];
  43.  
  44. $args = [];
  45. $key = 0;
  46. foreach ($sortings as $sorting) {
  47. $args[$key] = array_column($value, strtotime($sorting['field']));
  48. $args[$key + 1] = $sorting['direction'];
  49. $key += 2;
  50. }
  51. $args[] = $value;
  52.  
  53. call_user_func_array('array_multisort', $args);
  54.  
  55.  
  56. echo '<pre>';
  57. print_R($args);
  58. echo '</pre>';
  59. echo '=========';


wynik:

  1. (
  2. [0] =>
  3. [1] => 3
  4. [2] => Array
  5. (
  6. [0] => Array
  7. (
  8. [field_5c6fe44e60cb8] => 2019-05-13 11:22:15
  9. [field_5c6fe4ab60cb9] =>
  10. [field_5c6fe4fd60cba] => Kamil
  11. [field_5c6fe51260cbb] => 22
  12. )
  13.  
  14. [1] => Array
  15. (
  16. [field_5c6fe44e60cb8] => 2019-04-13 22:22:15
  17. [field_5c6fe4ab60cb9] =>
  18. [field_5c6fe4fd60cba] => Kamil
  19. [field_5c6fe51260cbb] => 333
  20. )
  21.  
  22. [2] => Array
  23. (
  24. [field_5c6fe44e60cb8] => 2019-02-13 13:22:15
  25. [field_5c6fe4ab60cb9] =>
  26. [field_5c6fe4fd60cba] => Kamil
  27. [field_5c6fe51260cbb] => 444
  28. )
  29.  
  30. [3] => Array
  31. (
  32. [field_5c6fe44e60cb8] => 2019-07-13 12:22:15
  33. [field_5c6fe4ab60cb9] =>
  34. [field_5c6fe4fd60cba] => Kamil
  35. [field_5c6fe51260cbb] => 111
  36. )
  37.  
  38. )
  39.  
  40. )


Także, spokojnie bardzo rzadko pojawiam się tutaj smile.gif
Go to the top of the page
+Quote Post
Pyton_000
post 13.05.2019, 21:45:06
Post #6





Grupa: Zarejestrowani
Postów: 8 068
Pomógł: 1414
Dołączył: 26.10.2005

Ostrzeżenie: (0%)
-----


Prawie wyszło wink.gif poza faktem że robiszz strtotime z nazwy kolumny a nie jej wartości smile.gif

Kod
$cols = array_map(function($item) {return strtotime($item);}, array_column($value, 'field_5c6fe44e60cb8'));
array_multisort($cols, SORT_DESC, $value);


Tu masz skondensowaną wersję.
Go to the top of the page
+Quote Post

Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Wersja Lo-Fi Aktualny czas: 28.03.2024 - 12:34