Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/296.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在PHP中获取每个数组值中的较低数字_Php_Arrays - Fatal编程技术网

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

在PHP中获取每个数组值中的较低数字,php,arrays,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": "4

我这里有一个数组

"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代码中从它们中得到较低级别的
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);
    }
}
但它只显示一个数组键

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

帮助我,谢谢

假设数组已按
ohp\u 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>";
}
?>

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

$min = array_reduce($data, function($min, $ohp) {
    return (!$min || $ohp['level'] < $min['level']) ? $ohp : $min;
});
$min=array\u reduce($data,function($min,$ohp){
返回(!$min | |$ohp['level']<$min['level'])?$ohp:$min;
});

哇,你的格式是。。。怪异:PI真的很抱歉,,但它只显示了一个数组键:(它应该返回一个具有最小级别的集合,这样你就可以得到它的id和其他东西。
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 ) 
$min = array_reduce($data, function($min, $ohp) {
    return (!$min || $ohp['level'] < $min['level']) ? $ohp : $min;
});