Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

InspiredPrynce's avatar

Array Flatten

I have data like this...


array:9 [▼
  0 => array:1 [▼
    3101625668 => 98.0
  ]
  1 => array:1 [▼
    3913126364 => 35.77
  ]
  2 => array:1 [▼
    3913058204 => 25.33
  ]
  3 => array:1 [▼
    3101372540 => 33.47
  ]
  4 => array:1 [▼
    3913752741 => 40.0
  ]
  5 => array:1 [▼
    3913120054 => 20.4
  ]
  6 => array:1 [▼
    3913998755 => 26.8
  ]
  7 => array:1 [▼
    3913861492 => 25.2
  ]
  8 => array:1 [▼
    3913881854 => 19.8
  ]
]

I want to flatten the array to something like this..

[$key => $value]

Please Laravel doesn't seem to be of much help.. Someone please help!

0 likes
13 replies
andreasbakir's avatar
Level 22

I wrote this code for you, I tested it and it works.

Route::get('/', function () {
    $myArray = [
        ['3101625668' => 98.0],
        ['3913126364' => 35.77],
        ['3913058204' => 25.33],
        ['3101372540' => 33.47],
        ['3913752741' => 40.0],
        ['3913120054' => 20.4],
        ['3913998755' => 26.8],
        ['3913861492' => 25.2]
    ];
    $sanitizedArray = sanitizeArray($myArray);
    return $sanitizedArray;
});

function sanitizeArray($array)
{
    static $newArray = [];
    foreach ($array as $key => $value) {
        if(is_array($value)) {
            sanitizeArray($value);
        } else {
            $newArray[$key] = $value;
        }
    }
    return $newArray;
}
1 like
Cronix's avatar

Did you test my code? I did with your data and it worked fine with just 2 lines of code...

rawilk's avatar

@andreasbakir - Why would you make a function to do this, when Laravel already provides this for you with what @cronix had suggested? Makes no sense...

andreasbakir's avatar

@Cronix the flatten method on a collection resets the array_keys of the array.

@wilk_randall And that is the exact reason why I made that little function, I do prefer using laravel helper methods over creating your own methods, but if you don't get the result you wish for you should make up your own solution.

1 like
InspiredPrynce's avatar

@andreasbakir this function doesn't seem to end once. When i use it twice or more, it returns the results in its previous usage. Its doesn't clear its memory, so to speak, how can this be fixed?

andreasbakir's avatar

@INSPIREDPRYNCE - That's strange can you give me an example of what you are trying to do, what the result is that you are expecting and what you actually get when you use my function ?

ATOM-Group's avatar

I just want to throw this out there, as the accepted answer takes the long way to get to what PHP basically gives you for free:

<?php

$data = [
    ['3101625668' => 98.0],
    ['3913126364' => 35.77],
    ['3913058204' => 25.33],
    ['3101372540' => 33.47],
    ['3913752741' => 40.0],
    ['3913120054' => 20.4],
    ['3913998755' => 26.8],
    ['3913861492' => 25.2]
];

$flattened = array_replace(...$data);

var_dump($flattened);

So what's going on here?

The documentation for array_replace() describes its behavior better than I can. But the important thing is that it takes N number of arrays as arguments, which is exactly the format of your data @InspiredPrynce

This means you could do this:

$flattened = array_replace($data[0], $data[1], $data[2], $data[3], $data[4], $data[5], $data[6], $data[7]);

But that obviously sucks, and we can do better.

That's where the splat operator comes in.

We can rewrite the above with some extremely terse code:

$flattened = array_replace(...$data); // same thing

The splat operator just says "use each element of the parent $data array as an argument for array_replace()."

Then array_replace() takes care of the rest.

This is pure PHP code. No Laravel involved.

The one caveat is that you need at least PHP 5.6 to use the splat operator.

2 likes

Please or to participate in this conversation.