Array help

Discussion in 'PHP' started by Dirty-Rockstar, Nov 6, 2008.

  1. #1
    Im trying to sort largest number to smallest, and print the name of each 'stat' I cant figure it out. I got it to print the values of each field, but That is not needed. its also sorting least to greatest. can anyone please help?

    
    $sql="select physical,mental,position,timing,distance,momentum from users where id='$users[id]'";
    $query=mysql_query($sql);
    $result=mysql_fetch_array($query);
    
    $statsInfo = array(
        'Physical Balance' => $result['physical'], //6.550
        'Mental Balance' => $result['mental'], //6.300
        'Position' => $result['position'], //10.190
        'Timing' => $result['timing'], //104.530
        'Distance' => $result['distance'], //1.825
        'Momentum' => $result['momentum'] //5.975
    );
    sort($statsInfo);
    foreach($statsInfo as $statName => $statValue) {
    	
    
    	print "$statsInfo[$statName]<br />";
    
    }
    
    /*
    prints 
    1.825
    5.975
    6.300
    6.550
    10.190
    104.530
    
    
    When what i need is. 
    the commented out is the value from above, 
    only this time sorted. by name of stat, 
    from greatest to least
    
    Timing   //104.530
    Position //10.190
    Physical Balance //6.550
    Mental Balance //6.300
    Momentum  //5.975
    Distance  //1.825
    */
    
    PHP:

    Ill even go with it printing least to greatest,
    I just want the name of the stat to print, not the value.
    I hope i was clear.
    Been driving me nuts.
    First experience with arrays
     
    Dirty-Rockstar, Nov 6, 2008 IP
  2. blueleaf

    blueleaf Active Member

    Messages:
    112
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    53
    #2
    Have you checked replacing the print statement with something like,

    print_r(array_keys($statsInfo));
     
    blueleaf, Nov 6, 2008 IP
  3. Dirty-Rockstar

    Dirty-Rockstar Guest

    Messages:
    252
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #3
    
    $statsInfo = array(
        'Physical Balance' => $result['physical'],
        'Mental Balance' => $result['mental'],
        'Position' => $result['position'],
        'Timing' => $result['timing'],
        'Distance' => $result['distance'],
        'Momentum' => $result['momentum']
    );
    sort($statsInfo);
    print_r(array_keys($statsInfo))
    
    
    PHP:
    shows me:

    Array ( [0] => 0 [1] => 1 [2] => 2 [3] => 3 [4] => 4 [5] => 5 )

    i want it to print this:
    (the comments are hypothetical values so you can see its printing greatest to least as the name of the stat, not the value. im completely lost)




    Timing //104.530
    Position //10.190
    Physical Balance //6.550
    Mental Balance //6.300
    Momentum //5.975
    Distance //1.825
     
    Dirty-Rockstar, Nov 6, 2008 IP
  4. shallowink

    shallowink Well-Known Member

    Messages:
    1,218
    Likes Received:
    64
    Best Answers:
    2
    Trophy Points:
    150
    #4
    
    $statsInfo = array("strength"=>4,"wisdom"=>4.3,"dexterity"=>2.2);
    
    arsort($statsInfo);
    foreach($statsInfo as $statName => $statValue) {
       echo  $statName . "<br />";
    }
    
    PHP:
    outputs:
    wisdom
    strength
    dexterity
     
    shallowink, Nov 6, 2008 IP
  5. Dirty-Rockstar

    Dirty-Rockstar Guest

    Messages:
    252
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #5
    yes, that did it. i took this example and implemented in my code and it worked like a charm. thanks
     
    Dirty-Rockstar, Nov 6, 2008 IP