Grouping multidimensional arrays in PHP by field

217

UPDATEvar_export version of the array HERE

I have the following array which I would like to group the elements/children by their UNILEVEL value:

array( 
    0 => array(
         "member_id" => 3,
         "unilevel" => 1, 
         "children" => array( 
             0 => array( 
                 "member_id" => 4,
                 "unilevel" => 2,
                 "children" => array( 
                     0 => array( 
                         "member_id" => 6,
                         "unilevel" => 3,
                         "children" => array( 
                             0 => array(
                                 "member_id" => 7,
                                 "unilevel" => 4 ) ) ) ) ) 
             1 => array( 
                 "member_id" => 9
                 "unilevel" => 2 ) ) )
    1 => array( 
        "member_id" => 5,
        "unilevel" => 1,
        "children" => array( 
            0 => array( 
                "member_id" => 8,
                "unilevel" => 2,
                "children" => array( 
                    0 => array( 
                        "member_id" => 10,
                        "unilevel" => 3 ) ) ) ) ) ) 

The Controller below has a function named,groupPerlevel which does the grouping, but it only groups the first parent right now, it's already a recursive function, I am not sure why it's not putting the second parent's elements on the unilevel groupings.

class TestController extends Controller {
    private $group = array();

    private function setGroup($value) {
        $this->group = $this->group + $value;
    }

    private function getGroup() {
        return $this->group;
    }

    public function create()
    {
        $this->groupPerlevel($tree);
        dd($this->getGroup());
    }

    private function groupPerlevel(array $items)
    {
        var_dump($items);
        $grouparr = $this->getGroup();

        $newkey = 0;
        $templevel = 1;

        foreach($items as $key => $val) {
            if($templevel == $val->unilevel) {
                $grouparr[$templevel][$newkey] = $val;
                $this->setGroup($grouparr);
            } else {
                if(isset($grouparr[$val->unilevel][$newkey])) {
                    $count = count($grouparr[$val->unilevel]);
                    $grouparr[$val->unilevel][$count] = $val;
                    $this->setGroup($grouparr);
                } else  {
                    $grouparr[$val->unilevel][$newkey] = $val;
                }
            }

            if(isset($val->children)) {
                $children = $val->children;
                unset($val->children);
                $this->groupPerlevel($children);   
            } 
            $newkey++;
        }
        $this->setGroup($grouparr);
    }
}

The following array would be my desired output. array(4) { [1] is the UNILEVEL (group) so all elements/children should be in their proper unilevel group based on their unilevel field value. But it only does that for the first parent, the second parent doesn't group.

array(
1 => array( 
    0 => array( 
        "member_id" => 3,
        "unilevel" => 1 ) )
4 => array( 
    0 => array( 
        "member_id" => 7,
        "unilevel" => 4 ) )
3 => array( 
    0 => array ( 
        "member_id" => 6,
        "unilevel" => 3 ) )
2 => array( 
    0 => array( 
        "member_id" => 4,
        "unilevel" => 2 
    1 => array( 
        "member_id" => 9
        "unilevel" => 2 )))
15

Answer

Solution:

Sorry for late response try this

$arr=array("shiva" => array("member_id" => "3","unilevel" => "1","children" => array("0" => array("member_id" => "4","unilevel" => "2","children" => array("0" => array("member_id" => "6", "unilevel" => "3", "children" => array( "0" => array( "member_id" => "7", "unilevel" => "4" ) ) ) ) ), "1" => array( "member_id" => "9", "unilevel" => "2" ))),"1" => array("member_id" => "5","unilevel" => "1","children" => array("0" => array("member_id" => "8","unilevel" => "2", "children" => array( "0" => array( "member_id" => "10", "unilevel" => "3" ) ) ) ) ) );


 function check($arr){

      if(is_array($arr)){

       foreach($arr as $arr1){

         foreach($arr1 as $arr2){

           if(is_array($arr2)){

             return true;               

           }

         }

       }   

     }
     else{

      return false;       

     }

      return false; 

  } 

$i=0;
while(check($arr)){

  foreach($arr as $arr1key=>$arr1val){

    foreach($arr1val as $arr2key=>$arr2val){

         if(is_array($arr2val)){

        $arr[]=$arr[$arr1key][$arr2key];

        unset($arr[$arr1key][$arr2key]);


      }        

   }      


  }  

}






foreach($arr as $arr1key=>$arr1val)
{

  if(count($arr1val)==0){
    unset($arr[$arr1key]);
  }    
}

//print_r($arr);

$result=array();
$values=array();


foreach($arr as $arr1key=>$arr1val)
{

  if(((array_key_exists("unilevel", $arr1val)) || (array_key_exists("member_id", $arr1val)))==true)
  {


      $key=$arr1val['unilevel'];
      $result[0][$key][]=array("unilevel"=>$arr1val["unilevel"],"member_id"=>$arr1val["member_id"]);


  }


}

print_r($result);

Result:

Array ( [0] => Array ( [1] => Array ( [0] => Array ( [unilevel] => 1 [member_id] => 3 ) [1] => Array ( [unilevel] => 1 [member_id] => 5 ) ) [2] => Array ( [0] => Array ( [unilevel] => 2 [member_id] => 4 ) [1] => Array ( [unilevel] => 2 [member_id] => 9 ) [2] => Array ( [unilevel] => 2 [member_id] => 8 ) ) [3] => Array ( [0] => Array ( [unilevel] => 3 [member_id] => 6 ) [1] => Array ( [unilevel] => 3 [member_id] => 10 ) ) [4] => Array ( [0] => Array ( [unilevel] => 4 [member_id] => 7 ) ) ) )

People are also looking for solutions to the problem: Change order of JSON in PHP without key/value pair

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.