[英]sort this array from highest to lowest
so i make an apriori algorithm, and this is the result.所以我做了一个先验算法,这就是结果。
this is the code where i get this array from.这是我从中获取此数组的代码。
$count = array_count_values($arrayCount);
$array = [];
foreach($count as $key => $value){
$confidence = ($value/$count_antecedent);
$support = ($value/$count_all_transactions);
$isi = [
$key=>$confidence*$support
];
array_push($array,$isi);
}
return $array;
i want to sort it from highest to lowest but also keeping the key.我想从高到低对它进行排序,但也要保留密钥。
if this array is sorted all i need is to take 4 highest number.如果这个数组是排序的,我只需要取 4 个最高的数字。
it will look like this (if sorted):它看起来像这样(如果已排序):
$i = 0;
foreach($sortedArray as $key => $value){
if($i == 0){
$product1 == product::find($key);
}
else if($i == 1){
$product2 = product::find($key);
}
else if($i == 2){
$product3 = product::find($key);
}
else if($i == 3){
$product4 = product::find($key);
}
$i++;
}
use arsort($array)
function.使用
arsort($array)
function。
It's all you need.这就是你所需要的。
so in order to order the array i need to use usort where the key name should be the same in every array.因此,为了对数组进行排序,我需要使用 usort,其中每个数组中的键名都应该相同。
so i changed the $isi variable to所以我将 $isi 变量更改为
$isi = ["value"=>$confidence*$support,"key"=>$key];
and then use this usort function to order the array然后使用这个 usort function 来订购阵列
usort($array,function($a,$b){
return $a["value"] < $b["value"];
})
and then to get 4 highest product from db there slighly change to the code然后从 db 获得 4 个最高产品,稍微更改代码
$i = 0;
foreach($array as $key => $value){
if($i == 0){
$product1 = product::find($value['key']);
}
else if($i == 1){
$product2 = product::find($value['key']);
}
else if($i == 2){
$product3 = product::find($value['key']);
}
else if($i == 3){
$product4 = product::find($value['key']);
}
$i++;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.