Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [php]Sortowanie złozonej tablicy wg wartości jednego pola..
Forum PHP.pl > Forum > Przedszkole
kukix
Witam.

Posiadam poniższa tablice, potzebvuje ją posortowac wg pola.. [sell-form-pos] w przypadku gdy pole to ma wartość 0 sortujemy wg alfabetu..

Czyli najpierw trzeba posortować wg pola [sell-form-title] a póxniej jeszcze wg pola [sell-form-pos]


  1. (
  2. [sell-form-fields] => Array
  3. (
  4. [0] => stdClass Object
  5. (
  6. [sell-form-id] => 4537
  7. [sell-form-title] => Chipset
  8. [sell-form-cat] => 63018
  9. [sell-form-type] => 4
  10. [sell-form-res-type] => 2
  11. [sell-form-def-value] => 0
  12. [sell-form-opt] => 8
  13. [sell-form-pos] => 0
  14. [sell-form-length] => 3
  15. [sell-min-value] => 0.00
  16. [sell-max-value] => 140.00
  17. [sell-form-desc] => -- Wybierz -- |AMD 740G|AMD 760G|AMD 770|AMD 785G|AMD 790X|AMD 790FX|AMD 790GX|AMD 870|AMD 880G|AMD 890FX|AMD 890GX|nVidia 6150SE/nForce430|nVidia 7025/nForce630a|nVidia nForce 520LE|Inny
  18. [sell-form-opts-values] => 0|10|20|30|40|50|60|70|80|90|100|110|115|120|130|140
  19. [sell-form-field-desc] => Komputery: Płyty główne: Socket AM3
  20. [sell-form-param-id] => 9003
  21. [sell-form-param-values] => 0|10|20|30|40|50|60|70|80|90|100|110|115|120|130|140
  22. [sell-form-parent-id] => 0
  23. [sell-form-parent-value] => 0
  24. [sell-form-unit] =>
  25. [sell-form-options] => 0
  26. )
  27.  
  28. [1] => stdClass Object
  29. (
  30. [sell-form-id] => 4538
  31. [sell-form-title] => Standard płyty
  32. [sell-form-cat] => 4228
  33. [sell-form-type] => 4
  34. [sell-form-res-type] => 2
  35. [sell-form-def-value] => 0
  36. [sell-form-opt] => 8
  37. [sell-form-pos] => 5
  38. [sell-form-length] => 2
  39. [sell-min-value] => 0.00
  40. [sell-max-value] => 60.00
  41. [sell-form-desc] => -- Wybierz -- |ATX|Micro ATX|Mini ITX|SSI|uATX|Inny
  42. [sell-form-opts-values] => 0|10|20|30|40|50|60
  43. [sell-form-field-desc] => Komputery: Płyty główne
  44. [sell-form-param-id] => 9004
  45. [sell-form-param-values] => 0|10|20|30|40|50|60
  46. [sell-form-parent-id] => 0
  47. [sell-form-parent-value] => 0
  48. [sell-form-unit] =>
  49. [sell-form-options] => 0
  50. )
  51.  
  52. [2] => stdClass Object
  53. (
  54. [sell-form-id] => 4539
  55. [sell-form-title] => Maksymalna ilość pamięci [GB]
  56. [sell-form-cat] => 4228
  57. [sell-form-type] => 3
  58. [sell-form-res-type] => 3
  59. [sell-form-def-value] => 0
  60. [sell-form-opt] => 8
  61. [sell-form-pos] => 20
  62. [sell-form-length] => 0
  63. [sell-min-value] => 0.00
  64. [sell-max-value] => 200.00
  65. [sell-form-desc] =>
  66. [sell-form-opts-values] =>
  67. [sell-form-field-desc] => Komputery: Płyty główne
  68. [sell-form-param-id] => 9020
  69. [sell-form-param-values] =>
  70. [sell-form-parent-id] => 0
  71. [sell-form-parent-value] => 0
  72. [sell-form-unit] =>
  73. [sell-form-options] => 0
  74. )
  75.  
  76. [3] => stdClass Object
  77. (
  78. [sell-form-id] => 4540
  79. [sell-form-title] => Rodzaj pamięci
  80. [sell-form-cat] => 4228
  81. [sell-form-type] => 6
  82. [sell-form-res-type] => 2
  83. [sell-form-def-value] => 0
  84. [sell-form-opt] => 8
  85. [sell-form-pos] => 10
  86. [sell-form-length] => 1
  87. [sell-min-value] => 0.00
  88. [sell-max-value] => 7.00
  89. [sell-form-desc] => DDR|DDR2|DDR3
  90. [sell-form-opts-values] => 1|2|4
  91. [sell-form-field-desc] => Komputery: Płyty główne
  92. [sell-form-param-id] => 9021
  93. [sell-form-param-values] => 16|64|512
  94. [sell-form-parent-id] => 0
  95. [sell-form-parent-value] => 0
  96. [sell-form-unit] =>
  97. [sell-form-options] => 0
  98. )
  99.  
  100. [4] => stdClass Object
  101. (
  102. [sell-form-id] => 4541
  103. [sell-form-title] => Typ pamięci
  104. [sell-form-cat] => 4228
  105. [sell-form-type] => 6
  106. [sell-form-res-type] => 2
  107. [sell-form-def-value] => 0
  108. [sell-form-opt] => 8
  109. [sell-form-pos] => 15
  110. [sell-form-length] => 5
  111. [sell-min-value] => 0.00
  112. [sell-max-value] => 16383.00
  113. [sell-form-desc] => DDR2 533|DDR2 667|DDR2 800|DDR2 1066|DDR2 1200|DDR3 1066|DDR3 1333|DDR3 1600|DDR3 1800|DDR3 2000|DDR3 2133|DDR3 2200|DDR3 2600+|Inny
  114. [sell-form-opts-values] => 1|2|4|8|16|32|64|128|256|512|1024|2048|4096|8192
  115. [sell-form-field-desc] => Komputery: Płyty główne
  116. [sell-form-param-id] => 9022
  117. [sell-form-param-values] => 4|8|32|256|1024|2048|8192|32768|131072|262144|524288|1048576|2097152|4194304
  118. [sell-form-parent-id] => 0
  119. [sell-form-parent-value] => 0
  120. [sell-form-unit] =>
  121. [sell-form-options] => 0
  122. )
  123.  
  124. [5] => stdClass Object
  125. (
  126. [sell-form-id] => 4542
  127. [sell-form-title] => Złącza
  128. [sell-form-cat] => 4228
  129. [sell-form-type] => 6
  130. [sell-form-res-type] => 2
  131. [sell-form-def-value] => 0
  132. [sell-form-opt] => 8
  133. [sell-form-pos] => 25
  134. [sell-form-length] => 2
  135. [sell-min-value] => 0.00
  136. [sell-max-value] => 31.00
  137. [sell-form-desc] => DVI|FireWire|HDMI|SPDIF|USB 3.0
  138. [sell-form-opts-values] => 1|2|4|8|16
  139. [sell-form-field-desc] => Komputery: Płyty główne
  140. [sell-form-param-id] => 9040
  141. [sell-form-param-values] => 4|128|256|512|8192
  142. [sell-form-parent-id] => 0
  143. [sell-form-parent-value] => 0
  144. [sell-form-unit] =>
  145. [sell-form-options] => 0
  146. )
  147.  
  148. [6] => stdClass Object
  149. (
  150. [sell-form-id] => 20022
  151. [sell-form-title] => Stan
  152. [sell-form-cat] => 4228
  153. [sell-form-type] => 4
  154. [sell-form-res-type] => 2
  155. [sell-form-def-value] => 0
  156. [sell-form-opt] => 1
  157. [sell-form-pos] => 0
  158. [sell-form-length] => 1
  159. [sell-min-value] => 0.00
  160. [sell-max-value] => 2.00
  161. [sell-form-desc] => -- Wybierz -- |Nowy|Używany
  162. [sell-form-opts-values] => 0|1|2
  163. [sell-form-field-desc] => globalny \"Stan\"
  164. [sell-form-param-id] => 11323
  165. [sell-form-param-values] => 0|1|2
  166. [sell-form-parent-id] => 0
  167. [sell-form-parent-value] => 0
  168. [sell-form-unit] =>
  169. [sell-form-options] => 0
  170. )
  171.  
  172. )
  173.  
  174. [ver-key] => 29596301
  175. [ver-str] => 1.0.38
  176. )



W jaki sposób mozna takie sortowanie osiągnąc? Znalazłem listę funkcji do sortowania tablic, ale nie wiem, którą funkcją można posortowac tą tabluice http://in.php.net/manual/en/array.sorting.php

koras140
Nie wiem czy przypadkiem nie powinieneś użyć nawet usort() czyli zdefiniować swoje własne sortowanie. Robi się to w ten sposób, iż funkcji usort ma dwa argumenty, pierwszy to tablica którą chcesz posortować, a drugi to funkcja za pomocą której sortujesz. Funkcję tę tworzysz, musi mieć ona dwa argumenty (wiadomo dwa elementy, które porównujesz) i musi zwracać wartość ujemną, zero lub dodatnią (argument pierwszy mniejszy, równy lub większy).

To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.