i have array here
"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"
}
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
Here's how you can get minimal object of one array:
$min = array_reduce($data, function($min, $ohp) {
return (!$min || $ohp['level'] < $min['level']) ? $ohp : $min;
});