Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> [HTML][PHP]Sortowanie multidimensional array
goartur
post
Post #1





Grupa: Zarejestrowani
Postów: 233
Pomógł: 27
Dołączył: 19.10.2014

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


Mam
  1. {
  2. "Zuiderlicht College": {
  3. "id": 71,
  4. "name": "Zuiderlicht College",
  5. "score": 3,
  6. "fav": 0,
  7. "priority": 2
  8. },
  9. "Winford Amsterdam": {
  10. "id": 70,
  11. "name": "Winford Amsterdam",
  12. "score": 5,
  13. "fav": 0,
  14. "priority": 3
  15. },
  16. "Wellantcollege Linnaeus": {
  17. "id": 69,
  18. "name": "Wellantcollege Linnaeus",
  19. "score": 2,
  20. "fav": 0,
  21. "priority": 0
  22. },
  23. "Wellantcollege Amsterdam Sloten": {
  24. "id": 68,
  25. "name": "Wellantcollege Amsterdam Sloten",
  26. "score": 2,
  27. "fav": 0,
  28. "priority": 0
  29. },
  30. }


Musze posortowac ta array na podstawie klucza "score"
Lecz nie mam zadnego pomyslu.. Pomocy
Go to the top of the page
+Quote Post
Pyton_000
post
Post #2





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

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


http://php.net/manual/en/function.array-multisort.php - #3
Go to the top of the page
+Quote Post
nospor
post
Post #3





Grupa: Moderatorzy
Postów: 36 557
Pomógł: 6315
Dołączył: 27.12.2004




Po pierwsze: to nie array tylko obiekt JSON
By zamienic to na array to musisz uzyc json_decode

Po drugie:
jak juz bedziesz mial array to wowczas manual php->usort()
Go to the top of the page
+Quote Post
goartur
post
Post #4





Grupa: Zarejestrowani
Postów: 233
Pomógł: 27
Dołączył: 19.10.2014

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


Tak zgadza sie to jest json(tylko tak to przedstawiam) musze posortowac moja array, problem mam wlasnie dokladnienie z.array-multisort ktory mi nie dziala nie wiem dlaczego nie mam pojecia jak to posortowac.
Go to the top of the page
+Quote Post
nospor
post
Post #5





Grupa: Moderatorzy
Postów: 36 557
Pomógł: 6315
Dołączył: 27.12.2004




Temu uzyj usort ktore jest o niebo latwiejsze w uzyciu (IMG:style_emoticons/default/smile.gif)
Go to the top of the page
+Quote Post
goartur
post
Post #6





Grupa: Zarejestrowani
Postów: 233
Pomógł: 27
Dołączył: 19.10.2014

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


Uzylem tak jak mowiles.

Dzieki wielkie dziala.
Go to the top of the page
+Quote Post
Pyton_000
post
Post #7





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

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


@nospor Really?

  1. array_multisort(array_column($array, 'score'), SORT_DESC, $array)
Go to the top of the page
+Quote Post
nospor
post
Post #8





Grupa: Moderatorzy
Postów: 36 557
Pomógł: 6315
Dołączył: 27.12.2004




@Pyton my friend. Ty mi tego nie tlumacz, ale widac dla goartura usort jest banalniejsze (IMG:style_emoticons/default/tongue.gif)
Go to the top of the page
+Quote Post
Pyton_000
post
Post #9





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

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


a w PHP7 jeszcze łatwiejsze (IMG:style_emoticons/default/biggrin.gif) :

  1. usort($array, function($v1, $v2) { return $v1['score'] <=> $v2['score']; });
Go to the top of the page
+Quote Post
nospor
post
Post #10





Grupa: Moderatorzy
Postów: 36 557
Pomógł: 6315
Dołączył: 27.12.2004




No ba... w php7 wszystko jest latwiejsze (IMG:style_emoticons/default/biggrin.gif)
Go to the top of the page
+Quote Post

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

 



RSS Aktualny czas: 23.08.2025 - 11:17