Search code examples
phparraysmultidimensional-arrayflattenarray-column

Convert array of single-element arrays to a one-dimensional array


I have this kind of an array containing single-element arrays:

$array = [[88868], [88867], [88869], [88870]];

I need to convert this to one dimensional array.

Desired output:

[88868, 88867, 88869, 88870]

Is there any built-in/native PHP functionality for this array conversion?


Solution

  • For your limited use case, this'll do it:

    $oneDimensionalArray = array_map('current', $twoDimensionalArray);
    

    This can be more generalized for when the subarrays have many entries to this:

    $oneDimensionalArray = call_user_func_array('array_merge', $twoDimensionalArray);