[英]Get the highest, second highest and the lowest from the multidimensional array based on another value
In my code, I am getting an array like below 在我的代码中,我得到如下数组
Array
(
[12247] => stdClass Object
(
[wid] => 12247
[uid] => 1626
[timestamp] => 1482021161
[weight_value] => 63.9
[nid] => 1195487
[source] => 0
[weight_entered] =>
)
[34843] => stdClass Object
(
[wid] => 34843
[uid] => 1632
[timestamp] => 1485298453
[weight_value] => 72.5
[nid] => 1206019
[source] => 8176
[weight_entered] =>
)
[35316] => stdClass Object
(
[wid] => 35316
[uid] => 1632
[timestamp] => 1485723529
[weight_value] => 54.2
[nid] => 1209357
[source] => 0
[weight_entered] =>
)
[35177] => stdClass Object
(
[wid] => 35177
[uid] => 1632
[timestamp] => 1485559176
[weight_value] => 53.3
[nid] => 1209357
[source] => 0
[weight_entered] =>
)
[12248] => stdClass Object
(
[wid] => 12248
[uid] => 1633
[timestamp] => 1481662016
[weight_value] => 56.6
[nid] => 1209357
[source] => 0
[weight_entered] =>
)
[12249] => stdClass Object
(
[wid] => 12249
[uid] => 1635
[timestamp] => 1479839680
[weight_value] => 54
[nid] => 1209357
[source] => 0
[weight_entered] =>
)
)
The array is order by the nid, timestamp, wid in descending order. 数组是按nid,timestamp,wid降序排列的。 I need to get an output array which will contain only the latest, second latest and the first weight values with the nid as an index.
我需要获得一个输出数组,其中将仅包含最新的,第二个最新的和第一个权重值,并以nid作为索引。 So that I will get the details of a particular nid only.
这样我就只能获得特定nid的详细信息。
The output which I think about is like below and the logic behind that is 我考虑的输出如下所示,其背后的逻辑是
These multidimensional arrays have a value
nid
.这些多维数组的值为
nid
。 I need to get the latest, second latest and the firstweight_value
from a particularnid
.我需要从特定的
nid
获取最新的,第二最新的和第一个weight_value
。 As the array is already sorted in descending order of time, I just need to fetch the first, second and last values from each inner array having commonnid
由于数组已经按照降序排列,所以我只需要从每个具有相同
nid
内部数组中获取第一个,第二个和最后一个值
Array
(
[1195487] => stdClass Object
(
[latest_weight] => 63.9
[second_latest_weight] =>
[first_weight] =>
)
[1206019] => stdClass Object
(
[latest_weight] => 72.5
[second_latest_weight] =>
[first_weight] =>
)
[1209357] => stdClass Object
(
[latest_weight] => 54.2
[second_latest_weight] => 53.3
[first_weight] => 54
)
)
I tried the code, but I am stuck up by not getting the proper logic to apply. 我尝试了代码,但是由于没有获得适用的逻辑而陷入困境。 Please help.
请帮忙。
The code which I am trying is given below. 我正在尝试的代码如下。 But it is not full and not correct too.
但是它并不完整,也不正确。
if(!empty($history_details)){
$last_weight = 0;
$second_last_weight = 0;
$first_weight = 0;
$prev_nid = '';
$prev_wid = '';
$prev_count = 1;
foreach($history_details as $single_history){
if(empty($weight_array[$single_history -> nid]['field_weight_format'])){
$current_weight = $weight_array[$single_history -> nid]['field_weight_value']." lbs";
}
else{
$current_weight = str_replace('-', ' ', $weight_array[$single_history -> nid]['field_weight_format']);
}
if($prev_nid == '' || $prev_nid != $single_history -> nid){
$last_weight = $single_history -> weight_value;
if($prev_nid != $single_history -> nid){
$prev_count = 1;
}
}
else if($prev_count === 2){
$second_last_weight = $single_history -> weight_value;
$first_weight = $single_history -> weight_value;
}
else if($prev_count > 2 && $prev_nid != $single_history -> nid){
$first_weight = $history_details[$prev_wid] -> weight_value;
}
$prev_count++;
$prev_nid = $single_history -> nid;
$prev_wid = $single_history -> wid;
}
}
You can just sort by the weight within a group I guess. 您可以按组中的权重排序。
function findInGroup($outerArray, $targetGroup) {
$array = array_filter($outerArray, function ($item) use ($targetGroup) {
return $item->nid == $targetGroup;
});
uasort($array,function ($a,$b) {
return $a->weight_value<=>$b->weight_value;
});
$first = reset($array);
$second = next($array);
$last = end($array);
return [ $first, $second, $last ];
}
Then you can do: 然后,您可以执行以下操作:
findInGroup($history_details,1209357);
Or you can do it for all groups via: 或者,您可以通过以下方式为所有组做到这一点:
$keys = array_unique(array_map(function ($item) {
return $item->nid;
}, $history_details); //All distinct keys
$pluckedValues = array_map(function ($id) use ($history_details) {
return findInGroup($history_details, $id);
}, $keys); //Just the values for each distinct key
$values = array_combine($keys, $pluckedValues); //Combined with the key.
Of course this is not an optimal solution but would be a place to start. 当然,这不是最佳解决方案,而是一个起点。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.