Ordinare array di array
-
Salve a tutti!
Avrei bisogno di un consiglio tecnico...
vorrei ordinare un array composto da vari campi:[B]array[/B][COLOR=#000000] [/COLOR][I](size=12)[/I] 0 [COLOR=#888a85]=>[/COLOR] [B]object[/B]([I]stdClass[/I])[[I]4[/I]] [I]public[/I] 'idcat' [COLOR=#888a85]=>[/COLOR] int [COLOR=#4e9a06]8[/COLOR] [I]public[/I] 'dscLng' [COLOR=#888a85]=>[/COLOR] string [COLOR=#cc0000]''[/COLOR] [I](length=0)[/I] [I]public[/I] 'ordVis' [COLOR=#888a85]=>[/COLOR] int [COLOR=#4e9a06]10[/COLOR] 1 [COLOR=#888a85]=>[/COLOR] [B]object[/B]([I]stdClass[/I])[[I]5[/I]] [I]public[/I] 'idcat' [COLOR=#888a85]=>[/COLOR] int [COLOR=#4e9a06]19[/COLOR] [I]public[/I] 'dscLng' [COLOR=#888a85]=>[/COLOR] string [COLOR=#cc0000]''[/COLOR] [I](length=0)[/I] [I]public[/I] 'ordVis' [COLOR=#888a85]=>[/COLOR] int [COLOR=#4e9a06]30[/COLOR] 2 [COLOR=#888a85]=>[/COLOR] [B]object[/B]([I]stdClass[/I])[[I]6[/I]] [I]public[/I] 'idcat' [COLOR=#888a85]=>[/COLOR] int [COLOR=#4e9a06]20[/COLOR] [I]public[/I] 'dscLng' [COLOR=#888a85]=>[/COLOR] string [COLOR=#cc0000]''[/COLOR] [I](length=0)[/I] [I]public[/I] 'ordVis' [COLOR=#888a85]=>[/COLOR] int [COLOR=#4e9a06]40[/COLOR] [COLOR=#222222][FONT=Verdana]vorrei appunto ordinare tale array per 'ordVis' prima di utilizzarlo in un foreach!Grazie in anticipo

-
Ciao anu,
postresti cercare un algoritmo di ordinamento (merge sort o quicksort, ad esempio) ed implementarlo a dovere nel tuo codice.
-
-
Hai bisogno di usort.
Nel tuo esempio sono già ordinati comunque questo funziona$a = array( array(8,'',10), array(19,'',30), array(20,'',40) ); function cmp($a, $b){ if($a[2] == $b[2]) return false; return ($a[2] < $b[2]) ? -1 : 1; } usort($a,"cmp"); print_r($a); -
-
Grazie a tutti per le risposte,
quello che cercavo tuttavia era questo:<?php $test = array(); $test[0]['name'] = 'zoe'; $test[0]['email'] = '[email protected]'; $test[0]['phone'] = '123-123-1234'; $test[0]['trick'] = 'mezopia'; $test[1]['name'] = 'Amanda'; $test[1]['email'] = '[email protected]'; $test[1]['phone'] = '123-123-1235'; $test[1]['trick'] = 'youarecool'; $test[2]['name'] = 'john'; $test[2]['email'] = '[email protected]'; $test[2]['phone'] = '123-123-3333'; $test[2]['trick'] = 'goneinanhour'; print_r(columnSort($test, 'name')); function columnSort($unsorted, $column) { $sorted = $unsorted; for ($i=0; $i < sizeof($sorted)-1; $i++) { for ($j=0; $j<sizeof($sorted)-1-$i; $j++) if ($sorted[$j][$column] > $sorted[$j+1][$column]) { $tmp = $sorted[$j]; $sorted[$j] = $sorted[$j+1]; $sorted[$j+1] = $tmp; } } return $sorted; } ?>trovato su php.net
Spero possa servire ad altri!
Ciao! Sembra che tu sia interessato a questa conversazione, ma non hai ancora un account.
Stanco di dover scorrere gli stessi post a ogni visita? Quando registri un account, tornerai sempre esattamente dove eri rimasto e potrai scegliere di essere avvisato delle nuove risposte (tramite email o notifica push). Potrai anche salvare segnalibri e votare i post per mostrare il tuo apprezzamento agli altri membri della comunità.
Con il tuo contributo, questo post potrebbe essere ancora migliore 💗
Registrati Accedi
