0

I have an eloquent object with data from the model which I need to convert to an array and have all indexes from the relations at the same array depth.

I tried array_flatten(), map(), filter() and other PHP methods but couldn't get it working correctly.

How I get my relations:

    $data = Person::whereIn('id', $this->id[0])->where('school_id', '=', 
    $this->school)->with(['personinfo'=>function ($query) {
        $query->select(
            'person_id',
            'general_info',
        );
    }, 'trades'=>function ($query) {
        $query->select('person_id', 'trade_id')->with('trades');
    }, 'measurements'=>function ($query) {
        $query->select(
            'person_id',
            'measuring_point_1',
            'measuring_point_1_date',
        );
    }])->get();

    return $data->toArray();

What results in the array below, this is as close as I could get using different methods.

This is the outcome of the return function:

array:3 [
  1 => array:17 [
    "school_id" => 6
    "birth_date" => null
    "sex_id" => 1
    "phone_number" => 62452676867897
    "mobile_number" => 62398356786787
    "email" => "example@example.com"
    "personinfo" => array:5 [
      "person_id" => 21
      "general_info" => null
      "health_info" => null
    ]
    "trades" => array:3 [
      "person_id" => 21
      "trade_id" => 2
      "trades" => array:8 [
        "school_id" => 2
        "name" => "blablabla"
      ]
    ]
    "measurements" => array:7 [
      "person_id" => 21
      "measuring_point_1" => null
      "measuring_point_1_date" => null
    ]
  ]
];

I need the array to be like this:

array:3 [
  1 => array:17 [
    "school_id" => 6
    "birth_date" => null
    "sex_id" => 1
    "phone_number" => 624176676867897
    "mobile_number" => 649498356786787
    "email" => "example@example.com"
    "person_id" => 21
    "general_info" => null
    "health_info" => null
    "person_id" => 21
    "trade_id" => 2
    "school_id" => 2
    "name" => "blablabla"
    "person_id" => 21
    "measuring_point_1" => null
    "measuring_point_1_date" => null
    ]
  ]
];

Basically, I need to convert the multidimensional array to a zero depth array.

Any help would be appreciated.

PrakashG
  • 1,642
  • 5
  • 20
  • 30
DutchPrince
  • 333
  • 2
  • 16

2 Answers2

2

You can use custom of array flatten with merging the inner with array-merge as:

function arrayFlatten($array) {
   $res = array();
   foreach ($array as $k => $v) {
       if (is_array($v)) $return = array_merge($res, array_flatten($v)); // add recursively
       else $res[$k] = $v; // just add
   }
   return $res;
}

Now just return arrayFlatten($data->toArray());

Simple live example

dWinder
  • 11,597
  • 3
  • 24
  • 39
  • Please help with the housekeeping by closing duplicate questions instead of answering. https://stackoverflow.com/a/14972389/2943403 – mickmackusa Jul 02 '19 at 11:36
  • i don't think this is a duplicate question, since this is a laravel related question and not specifically multidimensional array's. You suggested the use of QueryBuilder right instead of modifying the outcome right? @mickmackusa – DutchPrince Jul 03 '19 at 13:09
  • The accepted answer is plucked directly from an answer on the dupe page. Borderline plagarism. – mickmackusa Jul 03 '19 at 13:12
0

How i made it work using dWinder's answer

       $data = Person::whereIn('id', $this->id[0])->where('school_id', '=', $this->school)->with(['personinfo'=>function ($query) {
        $query->select(
            'person_id',
            'general_info',
        );
    }, 'trades'=>function ($query) {
        $query->select('person_id', 'trade_id')->with('trades');
    }, 'measurements'=>function ($query) {
        $query->select(
            'person_id',
            'measuring_point_1',
            'measuring_point_1_date',
        );
    }])->get();


function array_flatten($array)
    {
        $return = array();
        foreach ($array as $key => $value) {
            if (is_array($value)) {
                $return = array_merge($return, array_flatten($value));
            } else {
                $return[$key] = $value;
            }
        }
        return $return;
    }

    foreach ($data as $value) {
        $result[] = array_flatten($value->toArray());
    }


    return $result;
DutchPrince
  • 333
  • 2
  • 16