Pagina 1 di 2 1 2 ultimoultimo
Visualizzazione dei risultati da 1 a 10 su 11
  1. #1

    ordinare un array a 2 dimensioni

    Ciao a tutti
    Ho cercato su php.net le varie funzioni per ordinare un array, ma credo che il mio caso sia di difficile soluzione.

    C'è un esempio che rende il mio problema su php.net,ed è questo: http://it.php.net/manual/en/function...sort.php#59576

    Io vorrei fare un ordinamento ad esempio per nome in ordine alfabetico,per poi visualizzarlo correttamente.
    Il punto è che sto girando per php.net senza capire cosa devo fare.

    Come potrei fare?

  2. #2
    Utente di HTML.it L'avatar di marco80
    Registrato dal
    May 2005
    Messaggi
    1,357
    natcasesort(); Ordina in ordine alfabetico.
    Dal titolo pero' sembra che ti serve altro. Spiegaci meglio magari.

  3. #3
    Ho un array a due dimensioni, come quello descritto nell'esempio.

    Ho praticamente:
    $vettore[1]["nome"] = giorgio;
    $vettore[1]["cognome"] = rossi;
    $vettore[1]["citta"] = roma;

    $vettore[2]["nome"] = mario;
    $vettore[2]["cognome"] = albi;
    $vettore[2]["citta"] = milano;

    $vettore[3]...ecc ecc

    Ora io dovrei visualizzarli, di base li posso visualizzare facendo il count di $vettore e con un ciclo for.

    Ma io vorrei visualzizare i risultati ordinati alfabeticamente, o per nome, o per cognome o per citta.

  4. #4
    secondo me il modo + veloce per evitare funzioni assurde e lente è questo

    crei un array indice
    $orderby = 'citta';
    foreach($vettore as $key => $value){
    $indice[$key] = $value[$orderby];
    }

    in questo modo ordinando l'array indice puoi farci un'altro ciclo per estrarre le chiavi ordinate

    ciao

  5. #5
    Non mi funziona :|
    Ho copiato esattamente quello che hai scritto, ma nn funge. Mi restituisce il valore del nome e nn l'id da usare come indice

    Ho provato anche questo(adattato al mio caso,modificando $result e order):
    codice:
    foreach($result as $res)
         $sortAux[] = $res['order'];
    
    array_multisort($sortAux, SORT_ASC, $result);
    
    print_r($result);
    e anche modificandolo contro il bug
    codice:
    array_multisort(&$a, SORT_DESC, &$b);

  6. #6
    Utente di HTML.it
    Registrato dal
    Sep 2002
    Messaggi
    460
    Se i dati arrivano da una query, e' opportuno ordinare nella query.
    There are 10 types of people in the world - those who understand binary and those who don't.

  7. #7
    Alcune cose vengono inserite nell'array da database, altre invece contengono calcoli che mi è impossibile ordinare da query tipo la distanza che viene calcolata dopo aver estratto i dati

  8. #8
    se controlli il mio codice noterai che io tengo invariata la chiave dell'array, altrimenti non puoi risalire all'indice originario

  9. #9
    C'è l'esempio apposito, su array_multisort()

    <?php
    $data[] = array('volume' => 67, 'edition' => 2);
    $data[] = array('volume' => 86, 'edition' => 1);
    $data[] = array('volume' => 85, 'edition' => 6);
    $data[] = array('volume' => 98, 'edition' => 2);
    $data[] = array('volume' => 86, 'edition' => 6);
    $data[] = array('volume' => 67, 'edition' => 7);
    ?>

    In this example, we will order by volume descending, edition ascending.

    We have an array of rows, but array_multisort() requires an array of columns, so we use the the below code to obtain the columns, then perform the sorting.

    copy to clipboard
    <?php
    // Obtain a list of columns
    foreach ($data as $key => $row) {
    $volume[$key] = $row['volume'];
    $edition[$key] = $row['edition'];
    }

    // Sort the data with volume descending, edition ascending
    // Add $data as the last parameter, to sort by the common key
    array_multisort($volume, SORT_DESC, $edition, SORT_ASC, $data);
    ?>

    The dataset is now sorted, and will look like this:

    copy to clipboard
    volume | edition
    -------+--------
    98 | 2
    86 | 1
    86 | 6
    85 | 6
    67 | 2
    67 | 7

  10. #10
    Ok l'ho trovato anche io, inoltre c'è un esempio con una funzione già pronta.

    codice:
    <?php
    
      function arrayColumnSort()
      {
       $n = func_num_args();
       $ar = func_get_arg($n-1);
       if(!is_array($ar))
         return false;
    
       for($i = 0; $i < $n-1; $i++)
         $col[$i] = func_get_arg($i);
    
       foreach($ar as $key => $val)
         foreach($col as $kkey => $vval)
           if(is_string($vval))
             ${"subar$kkey"}[$key] = $val[$vval];
    
       $arv = array();
       foreach($col as $key => $val)
         $arv[] = (is_string($val) ? ${"subar$key"} : $val);
       $arv[] = $ar;
    
       call_user_func_array("array_multisort", $arv);
       return $ar;
      }
    
      $test["pete"]['points']=1;
      $test["pete"]['name']='Peter';
    
      $test["mike"]['points']=5;
      $test["mike"]['name']='Mike';
    
      $test["zoo"]['points']=2;
      $test["zoo"]['name']='John Zoo';
    
      $test["ab"]['points']=2;
      $test["ab"]['name']='John Ab';
    
      $test1 = $test;
    
      asort($test1);
    
      $test2 = arrayColumnSort("points", SORT_DESC, SORT_NUMERIC, "name", SORT_ASC, SORT_STRING, $test);
    
      print_r($test1); // asort
      print_r($test2); // arrayColumnSort
    
    ?>

    Però nn ho capito capito se devo defnire ogni volta tutte le colonne quando faccio questo

    $test2 = arrayColumnSort("points", SORT_DESC, SORT_NUMERIC, "name", SORT_ASC, SORT_STRING, $test);


    o basta anche una?
    grazie

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.