根据另一个值

时间:2017-11-07 12:44:24

标签: php arrays

在我的代码中,我得到一个如下所示的数组

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] => 
    )
)

数组按nid,timestamp,wid的顺序降序排列。我需要得到一个输出数组,其中只包含最新的,第二个最新的和第一个权重值,并将nid作为索引。所以我只会得到特定nid的详细信息。

我想到的输出如下所示,背后的逻辑是

  

这些多维数组的值为nid。我需要得到   最新的,第二个最新的和来自特定的第一个weight_value   nid。由于数组已经按时间降序排序,我   只需要从每个内部获取第一个,第二个和最后一个值   具有共同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
    )
) 

我尝试了代码,但由于没有得到适当的逻辑来应对,我感到困惑。请帮忙。

我正在尝试的代码如下。但它并不完整,也不正确。

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;

  }
}

1 个答案:

答案 0 :(得分:0)

你可以按照我猜的组内的重量进行排序。

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 ];
}

然后你可以这样做:

findInGroup($history_details,1209357);

或者您可以通过以下方式为所有群组执行此操作:

$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.

当然,这不是一个最佳解决方案,但可以作为一个起点。