Skip to content
Advertisement

Flatten laravel relationship collection / array

I am having problem with array convert I went use laravel collection using map Or php array

Array
(
    [0] => Array
        (
            [id] => 3487
            [title_fa] => Father
            [code] => 01
            [father_id] => 0
            [webmaster_id] => 8
            [grandchildren] => Array
                (
                    [id] => 3488
                    [title_fa] => Child 1
                    [code] => 02
                    [father_id] => 3487
                    [grandchildren] => 

                )

        )
     [1] => Array
        (
            [id] => 3489
            [title_fa] => Father 2
            [code] => 03
            [father_id] => 3488
            [grandchildren] => Array
                (
                    [id] => 3490
                    [title_fa] => Child 2
                    [code] => 04
                    [father_id] => 3489
                    [grandchildren] => 
                )

        )

)

array convert to array

Array
(
   [0] => Array
     (
            [title_fa] => Father
            [code] => 01
     )
   [1] => Array
     (
            [title_fa] => Father>Child1,
            [code] => 0102
     )
   [2] => Array
     (
            [title_fa] => Father2
            [code] => 03
     )
   [3] => Array
     (
            [title_fa] => Father2>Child2
            [code] => 0304
     )
)

IlluminateSupportCollection::filter()

 public function filter(callable $callback = null)

{

    if ($callback) {

        return new static(Arr::where($this->items, $callback));

    }



    return new static(array_filter($this->items));

}

Advertisement

Answer

Though the answer is already given but the output is not the desired output of the OP.

This can be solved using recursionlike this:

<?php
$codes=array();
$code="";
$title="";
$outerArray = array();

function callAgain($arr,&$outerArray,&$code,&$title){
   $check=0;
  foreach($arr as $value){
      if(is_array($value)){
         callAgain($value,$outerArray,$code,$title);
      }
      else{
        if($check==0){
            $code.=$arr['code'];
            if($title==""){
                $title.=$arr['title_fa']; 
            }
            else{
             $title.=">".$arr['title_fa'];
            }
            $outerArray[] = array ('title_fa'=>  $title,'code'=>$code);
         $check=1;    
        }  
      }

  }

}



$arr = [
        ['title_fa' => 'Father', 'code' => '02', 'grandchildren'=>[
            'title_fa' => 'Child 1', 'code' => '01', 'grandchildren'=>[
                'title_fa' => 'Child 2', 'code' => '01', 'grandchildren'=> 
                [
                'title_fa' => 'Child 3', 'code' => '01', 'grandchildren'=> '',
            ]
            ]
        ]] 
    ];


callAgain($arr,$outerArray,$code,$title);
print_r($outerArray);

Output:

Array
(
    [0] => Array
        (
            [title_fa] => Father
            [code] => 02
        )

    [1] => Array
        (
            [title_fa] => Father>Child 1
            [code] => 0201
        )

    [2] => Array
        (
            [title_fa] => Father>Child 1>Child 2
            [code] => 020101
        )

    [3] => Array
        (
            [title_fa] => Father>Child 1>Child 2>Child 3
            [code] => 02010101
        )

)
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement