Php 如何对两个数组值进行相应的分类
我在另一个页面中调用上述函数,如:Php 如何对两个数组值进行相应的分类,php,jquery,Php,Jquery,我在另一个页面中调用上述函数,如: This line of code adds two array values into $carRate variable: array_push($carRate,array($row['rental_type_value'],$row['rental_by_value'])); public function selectRate() { $sql="SELECT * FROM rental_by, rent
This line of code adds two array values into $carRate variable:
array_push($carRate,array($row['rental_type_value'],$row['rental_by_value']));
public function selectRate()
{
$sql="SELECT * FROM rental_by, rental_type WHERE rental_by.rental_type_id=rental_type.rental_type_id";
$stmt =connection::$pdo->prepare($sql);
$stmt->execute();
$carRate=array();
while($row = $stmt->fetch())
{
array_push($carRate,array($row['rental_type_value'],$row['rental_by_value']));
}
return $carRate;
}
并在通过ajax获取数据后通过jquery显示,如下面的内部成功函数所示:
$setting = new setting();
$selectRate=$setting->selectRate();
$json=array();
array_push($json,array("type"=>$carType,"maker"=>$carMaker,"rate"=>$selectRate));
echo json_encode($json);
这个输出是这样的:
["rental_days", "78"]
["rental_days", "56"]
["rental_days", "34"]
["rental_hour", "45"]
["rental_hour", "67"]
["rental_hour", "23"]
["rental_hour", "45"]
["rental_hour", "67"]
var categorized = {};
data.forEach(function(d){
if(!!categorized[d[0]]){
categorized[d[0]].push(d[1]);
} else {
categorized[d[0]] = [d[1]];
}
});
租赁天数:78租赁天数:56
租赁天数:34
租赁时间:45小时
租赁时间:67
租赁时间:23小时 租赁时间:45小时
租赁时间:67
预期结果: 租赁天数:78,56,34
租赁时间:45,67,23,45,67
有没有一种方法可以在php中对租金类型值和按值租金进行分类/排序?假设数据是这样返回的:
["rental_days", "78"]
["rental_days", "56"]
["rental_days", "34"]
["rental_hour", "45"]
["rental_hour", "67"]
["rental_hour", "23"]
["rental_hour", "45"]
["rental_hour", "67"]
var categorized = {};
data.forEach(function(d){
if(!!categorized[d[0]]){
categorized[d[0]].push(d[1]);
} else {
categorized[d[0]] = [d[1]];
}
});
您可以对数据客户端进行分类:
var data = [
['rental_days', '78'],
['rental_hour', '67'],
['rental_days', '56'],
['rental_hour', '45'],
['rental_days', '34'],
['rental_hour', '23'],
['rental_hour', '45'],
['rental_hour', '67']];
并将行添加到表中,如下所示:
["rental_days", "78"]
["rental_days", "56"]
["rental_days", "34"]
["rental_hour", "45"]
["rental_hour", "67"]
["rental_hour", "23"]
["rental_hour", "45"]
["rental_hour", "67"]
var categorized = {};
data.forEach(function(d){
if(!!categorized[d[0]]){
categorized[d[0]].push(d[1]);
} else {
categorized[d[0]] = [d[1]];
}
});
for(分类中的变量c){
$('table tr:last')。在(''+c++'+categorized[c].join()+'')之后;
};
看看这个