1

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"
  }

I need to compare each other array value and get lower number of level then get ohp_id of the lower level from each of them in PHP code. Here what i need to be:

"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"
  }

I know it need looping, tried:

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

But i don't know how to get ohp_id from lower level. I tried from @urmaul answer:

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

but it just show for one array key

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

Help me, thanks

1
  • 2
    Wow your formatting is... weird :P Commented Jul 20, 2016 at 8:27

2 Answers 2

1

Assuming that the array is already sorted by the ohp_id you can easily do it like this:

<?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>";
}
?>

Output:

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 ) 
Sign up to request clarification or add additional context in comments.

Comments

1

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

2 Comments

I'm really sorry,, but it just show one array key :(
It should return one set with minimal level so you can get it's id and stuff.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.