Visualizzazione dei risultati da 1 a 9 su 9
  1. #1
    Utente di HTML.it L'avatar di torrone
    Registrato dal
    Apr 2006
    residenza
    Padova
    Messaggi
    1,128

    array, trovare il piu grande

    ho un array strutturato così

    Codice PHP:
    array(1=>array("a" => $a_tot['1'], "b" => $a_perc['1']),
        
    2=>array("a" => $a_tot['2'], "b" => $a_perc['2']),
        
    3=>array("a" => $a_tot['3'], "b" => $a_perc['3']),
        
    4=>array("a" => $a_tot['4'], "b" => $a_perc['4']),
        
    5=>array("a" => $a_tot['5'], "b" => $a_perc['5']),
        
    6=>array("a" => $a_tot['6'], "b" => $a_perc['6']),
        
    7=>array("a" => $a_tot['7'], "b" => $a_perc['7']),
        
    8=>array("a" => $a_tot['8'], "b" => $a_perc['8']),
        
    9=>array("a" => $a_tot['9'], "b" => $a_perc['9']),
        
    10=>array("a" => $a_tot['10'], "b" => $a_perc['10']),
        
    11=>array("a" => $a_tot['11'], "b" => $a_perc['11']),
        
    12=>array("a" => $a_tot['12'], "b" => $a_perc['12']),
        
    13=>array("a" => $a_tot['13'], "b" => $a_perc['13']),
        
    14=>array("a" => $a_tot['14'], "b" => $a_perc['14']),
        
    15=>array("a" => $a_tot['15'], "b" => $a_perc['15']),
        
    16=>array("a" => $a_tot['16'], "b" => $a_perc['16']),
        
    17=>array("a" => $a_tot['17'], "b" => $a_perc['17']),
        
    18=>array("a" => $a_tot['18'], "b" => $a_perc['18']),
        
    19=>array("a" => $a_tot['19'], "b" => $a_perc['19']),
        
    20=>array("a" => $a_tot['20'], "b" => $a_perc['20']),
        
    21=>array("a" => $a_tot['21'], "b" => $a_perc['21']),
        
    22=>array("a" => $a_tot['22'], "b" => $a_perc['22']),
        
    23=>array("a" => $a_tot['23'], "b" => $a_perc['23']),
        
    24=>array("a" => $a_tot['24'], "b" => $a_perc['24'])); 
    dove $a_tot e $a_perc sono valori numerici.
    io dovrei trovare qual'è il valore $a_perc piu grande e trovare il valore piu grande trovare il suo numero

    esempio

    $a_perc piu grande è $a_perc['20'] e il suo numero è 20 (quello all'inizio dell'array) 20=>array("a" => $a_tot['20'], "b" => $a_perc['20']),

    come posso fare?

  2. #2
    Utente di HTML.it L'avatar di oronze
    Registrato dal
    Jun 2001
    Messaggi
    3,543
    qualcosa tipo:

    Codice PHP:
    $array tuo_array;
    $max $array[0]
    $value 0;
    for(
    $i=1$i<count($array); $i++){
      if(
    $array[$i]["b"] > $max){
        
    $max $array[$i]["b"];
        
    $value $i;
      }

    alla fine dovresti ottenere cosa cerchi

    No ai layout tabellari!

    Insulto libero: http://forum.html.it/forum/showthread.php?s=&postid=12524872#post12524872

  3. #3
    Utente di HTML.it L'avatar di torrone
    Registrato dal
    Apr 2006
    residenza
    Padova
    Messaggi
    1,128
    grazie questo è un esempio, dovrebbe uscire 3 ma mi da un errore

    Notice: Undefined offset: 0 in /var/www/test/test.php on line 8

    perchè 0 non c'è.. come faccio?

    Codice PHP:
    <?php

    $array 
    = array(1=>array("a" => 26.18"b" => 4.13),
        
    2=>array("a" => 26.20"b" => 4.15),
        
    3=>array("a" => 26.30"b" => 4.30),
        
    4=>array("a" => 26.25"b" => 4.25));

    $max $array[0];
    $value 0;
    for(
    $i=1$i<count($array); $i++){
      if(
    $array[$i]["b"] > $max){
        
    $max $array[$i]["b"];
        
    $value $i;
      }
    }

    ?>

  4. #4
    Utente di HTML.it L'avatar di oronze
    Registrato dal
    Jun 2001
    Messaggi
    3,543
    imposta come massimo l'elemento 1 e nel ciclo for parti da 2

    No ai layout tabellari!

    Insulto libero: http://forum.html.it/forum/showthread.php?s=&postid=12524872#post12524872

  5. #5
    Utente di HTML.it L'avatar di torrone
    Registrato dal
    Apr 2006
    residenza
    Padova
    Messaggi
    1,128
    Originariamente inviato da oronze
    imposta come massimo l'elemento 1 e nel ciclo for parti da 2
    ho fatto così ma nulla..

    Codice PHP:
    <?php

    $array 
    = array(1=>array("a" => 26.18"b" => 4.13), 
        
    2=>array("a" => 26.20"b" => 4.15), 
        
    3=>array("a" => 26.30"b" => 4.30), 
        
    4=>array("a" => 26.25"b" => 4.25)); 

    $max $array[1]; 
    $value 0
    for(
    $i=2$i<count($array); $i++){ 
      if(
    $array[$i]["b"] > $max){ 
        
    $max $array[$i]["b"]; 
        
    $value $i;
        echo 
    $max "
    $value;
      } 


    ?>

  6. #6
    Utente di HTML.it L'avatar di oronze
    Registrato dal
    Jun 2001
    Messaggi
    3,543
    Codice PHP:
    <?php

    $array 
    = array(1=>array("a" => 26.18"b" => 4.13), 
        
    2=>array("a" => 26.20"b" => 4.15), 
        
    3=>array("a" => 26.30"b" => 4.30), 
        
    4=>array("a" => 26.25"b" => 4.25)); 

    $max $array[1]; 
    $value 1
    for(
    $i=2$i<=count($array); $i++){ 
      if(
    $array[$i]["b"] > $max){ 
        
    $max $array[$i]["b"]; 
        
    $value $i;
      } 

    echo 
    $max "
    $value;
    ?>
    Così dovrebbe andare...nn ho modo di provarlo

    No ai layout tabellari!

    Insulto libero: http://forum.html.it/forum/showthread.php?s=&postid=12524872#post12524872

  7. #7
    Utente di HTML.it L'avatar di torrone
    Registrato dal
    Apr 2006
    residenza
    Padova
    Messaggi
    1,128
    mi stampa

    Array
    1

    che non è giusto..

  8. #8
    Utente di HTML.it L'avatar di oronze
    Registrato dal
    Jun 2001
    Messaggi
    3,543
    Originariamente inviato da torrone
    mi stampa

    Array
    1

    che non è giusto..
    perdonami...non avevo considerato che
    $max = $array[1];
    è un array...il valore è invece
    $max = $array[1]["b"];

    bastava fare 2 prove

    No ai layout tabellari!

    Insulto libero: http://forum.html.it/forum/showthread.php?s=&postid=12524872#post12524872

  9. #9

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.