Search code examples
phplaravellaravel-5eloquent

Convert array of objects to array of values in Laravel


I have below code in my laravel application controller:

$levels =  $request->level;
return $levels;

and it returns like this:

{
  1: [
    "9226"
  ],
  2: [
    "166"
  ]
}

which I need is return just values (9226,166) as array like below:

{
  "9226",
  "166"
}

How to get just values?


Solution

  • if array then you can use array_flatten()

    array_flatten($levels)
    

    if it is collection then

    $ar = $levels->flatten();
    $ar->all();  // get array