函数使用 array_multisort 但不知道如何指定键
Function using array_multisort but don't know how to specify key
对 PHP 很陌生,但找不到相关信息来帮助我。
基本上我正在编写一个函数来从我的 mysql 数据库中获取一天的赛马时间并进行排序。所以我引入了 3 个值 - racedate、racevenue 和 sectional。分段是这样我可以指定我是否想要他们的最后 200 米时间,他们最后 400 米时间等等。
我运行查询,然后把结果放到一个数组中。然后我想按 $sectional.
的值对该数组进行排序
我的问题是创建数组 $times 以使用 array_multisort。我能让它工作的唯一方法是对 'time200' 进行硬编码以指定要排序的内容。我真的希望能够按 $sectional 变量排序,因此我可以对 $sectional 传递的任何类型的列名(即 time200、time400、time600 等)使用此函数。
用 $sectional 而不是 time200 不起作用(这对你更了解你在做什么的人来说很明显)但我不明白为什么。
感谢您的帮助!
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;
}
看来你的方法不错。对我有用。
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}]
对 PHP 很陌生,但找不到相关信息来帮助我。
基本上我正在编写一个函数来从我的 mysql 数据库中获取一天的赛马时间并进行排序。所以我引入了 3 个值 - racedate、racevenue 和 sectional。分段是这样我可以指定我是否想要他们的最后 200 米时间,他们最后 400 米时间等等。
我运行查询,然后把结果放到一个数组中。然后我想按 $sectional.
的值对该数组进行排序我的问题是创建数组 $times 以使用 array_multisort。我能让它工作的唯一方法是对 'time200' 进行硬编码以指定要排序的内容。我真的希望能够按 $sectional 变量排序,因此我可以对 $sectional 传递的任何类型的列名(即 time200、time400、time600 等)使用此函数。
用 $sectional 而不是 time200 不起作用(这对你更了解你在做什么的人来说很明显)但我不明白为什么。
感谢您的帮助!
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;
}
看来你的方法不错。对我有用。
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}]