Search code examples
phpfunctionarray-multisort

Function using array_multisort but don't know how to specify key


pretty new to PHP here but can't find info to help me on this.

Basically I'm writing a function to grab race times for horses on one day from my mysql database and order them. So I pull in 3 values - racedate, racevenue, and sectional. Sectional is so I can specify if I want their last 200m time, their last 400m time or so on.

I run the query and then put the result into an array. I then want to sort that array by the value of $sectional.

My problem is creating the array $times to use the array_multisort. The only way I can get it to work is to hardcode the 'time200' to specify what to sort on. I really want to be able to sort by the $sectional variable, so I can use this function on any type of column name passed in by $sectional (ie time200, time400, time600 etc).

Putting $sectional instead of time200 doesn't work (which is prob obvious to you who know more of what you're doing) but I can't figure out why.

Thanks for any help!

  function sectionalrank($racedate, $racevenue, $sectional) {
    global $connection;

    $query   = "SELECT horsename, $sectional ";
    $query  .= "FROM runs ";
    $query  .= "WHERE racedate = '$racedate' ";
    $query  .= "AND venue = '$racevenue' ";
    $query  .= "AND $sectional > 0";

    $result = mysqli_query($connection, $query);

    $array = array();
    while($row = mysqli_fetch_assoc($result)) { 
        $array[] = $row;
    }

    $times = array();
    foreach ($array as $a) {
        $times[] = $a['time200'];
    }

    array_multisort($times, SORT_ASC, $array);

    return $array;

  }

Solution

  • Your approach appears to be good. It works for me.

    function sectionalrank( $racedate, $racevenue, $sectional )
        {
        // simulate database query
        $racedata[] = array('horsename' => 67, 'time200' => 2, 'time400' => 8);
        $racedata[] = array('horsename' => 86, 'time200' => 1, 'time400' => 3);
        $racedata[] = array('horsename' => 85, 'time200' => 6, 'time400' => 9);
        $racedata[] = array('horsename' => 98, 'time200' => 2, 'time400' => 1);
        $racedata[] = array('horsename' => 86, 'time200' => 6, 'time400' => 6);
        $racedata[] = array('horsename' => 67, 'time200' => 7, 'time400' => 4);
    
        $array = array();
        foreach ($racedata as $row)
            {
            $array[] = $row;
            }
    
        $times = array();
        foreach ($array as $a)
            {
            $times[] = $a[$sectional];
            }
    
        array_multisort($times, SORT_ASC, $array);
        return $array;
        }
    
    // Using $racedata to pass in the data.
    echo json_encode( sectionalrank( 'racedate', 'racevenue', 'time200' ) ) . "\n";
    echo json_encode( sectionalrank( 'racedate', 'racevenue', 'time400' ) ) . "\n";
    
    Output:
    [{"horsename":86,"time200":1,"time400":3},{"horsename":67,"time200":2,"time400":8},{"horsename":98,"time200":2,"time400":1},{"horsename":85,"time200":6,"time400":9},{"horsename":86,"time200":6,"time400":6},{"horsename":67,"time200":7,"time400":4}]
    [{"horsename":98,"time200":2,"time400":1},{"horsename":86,"time200":1,"time400":3},{"horsename":67,"time200":7,"time400":4},{"horsename":86,"time200":6,"time400":6},{"horsename":67,"time200":2,"time400":8},{"horsename":85,"time200":6,"time400":9}]