获取较低的数字在PHP中的每个数组值

时间:2016-07-20 08:25:33

标签: php arrays

我在这里有阵列

"data": [
 {
    "ohp_id": "40",
    "parent_ohp_id": "",
    "level": "1"
  },
 {
    "ohp_id": "42",
    "parent_ohp_id": "",
    "level": "2"
  },
  {
    "ohp_id": "45",
    "parent_ohp_id": "",
    "level": "5"
  },
  {
    "ohp_id": "46",
    "parent_ohp_id": "",
    "level": "5"
  },
  {
    "ohp_id": "47",
    "parent_ohp_id": "",
    "level": "5"
  }

我需要在PHP代码中比较每个其他数组值并获得较低数量的level,然后获取较低级别的ohp_id。这就是我需要的:

"data": [
 {
    "ohp_id": "40",
    "parent_ohp_id": "",
    "level": "1"
  },
 {
    "ohp_id": "42",
    "parent_ohp_id": "40",
    "level": "2"
  },
  {
    "ohp_id": "45",
    "parent_ohp_id": "42"
    "level": "5"
  },
  {
    "ohp_id": "46",
    "parent_ohp_id": "42",
    "level": "5"
  },
  {
    "ohp_id": "47",
    "parent_ohp_id": "42",
    "level": "5"
  }

我知道它需要循环,尝试过:

for ($i = 0; $i < count($arrPosition); $i++) {

    $hasPosition->loadHas($orgId, $arrPosition[$i]);

    if (!$hasPosition->id) {
        $hasPosition->level=$arrLevel[$i];
        $hasPosition->parent_ohp_id=<get ohp id from lower level>;
        $hasPosition->ohp_id=$ohp_id;
        $hasPosition->save();
    } else {
        if ($hasPosition->level!=$arrLevel[$i]) 
            $hasPosition->level=$arrLevel[$i];
        if ($hasPosition->seat!=$arrSeat[$i])
            $hasPosition->seat=$arrSeat[$i];

        $hasPosition->save(true);
    }
}

但我不知道如何从较低级别获取ohp_id。我试着从@urmaul回答:

$min = array_reduce($data, function($min, $ohp) {
    return (!$min || $ohp['level'] < $min['level']) ? $ohp : $min;
});

但它只显示一个数组键

Array
(
    [ohp_id] => 81
    [parent_ohp_id] =>
    [level] => 2
)

帮助我,谢谢

2 个答案:

答案 0 :(得分:1)

假设数组已经按ohp_id排序,您可以像这样轻松地执行此操作:

<?php

class OHP {
public $ohp_id;
public $parent_ohp_id;
public $level;
    public function __construct($ohpId,$parentId,$level){
        $this->ohp_id = $ohpId;
        $this->parent_ohp_id = $parentId;
        $this->level = $level;
    }
}

function sortByParentId(&$dataArr){
        $lastParentId = null;       
        for($j=0; $j + 1 < count($dataArr); $j++){              
            if($dataArr[$j+1]->level > $dataArr[$j]->level){
                $lastParentId = $dataArr[$j]->ohp_id;
            }
            $dataArr[$j+1]->parent_ohp_id = $lastParentId;
        }       
}



$data[] = new OHP(40,null,1); 
$data[] = new OHP(42,null,2); 
$data[] = new OHP(45,null,5); 
$data[] = new OHP(46,null,5); 
$data[] = new OHP(47,null,5); 


sortByParentId($data);


foreach($data as $currObj){
    print_r($currObj);
    echo "<br>";
}
?>

输出:

OHP Object ( [ohp_id] => 40 [parent_ohp_id] => [level] => 1 ) 
OHP Object ( [ohp_id] => 42 [parent_ohp_id] => 40 [level] => 2 ) 
OHP Object ( [ohp_id] => 45 [parent_ohp_id] => 42 [level] => 5 ) 
OHP Object ( [ohp_id] => 46 [parent_ohp_id] => 42 [level] => 5 ) 
OHP Object ( [ohp_id] => 47 [parent_ohp_id] => 42 [level] => 5 ) 

答案 1 :(得分:1)

以下是如何获得一个数组的最小对象:

$min = array_reduce($data, function($min, $ohp) {
    return (!$min || $ohp['level'] < $min['level']) ? $ohp : $min;
});