简体   繁体   中英

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. 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. So that I will get the details of a particular nid only.

The output which I think about is like below and the logic behind that is

These multidimensional arrays have a value nid . I need to get the latest, second latest and the first weight_value from a particular nid . 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 common 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM