gogic1
gogic1

Reputation: 111

How to merge large arrays in PHP?

I have 7 arrays of objects . Every object contains data. I need to merge these arrays together and I do that with:

$arrayTotal = $array1;
$arrayTotal = array_merge($arrayTotal,$array2)
$arrayTotal = array_merge($arrayTotal,$array3)
$arrayTotal = array_merge($arrayTotal,$array4)
...

All was working great until I had my total array of something like 700 items (each sub-array had 100 items ). Apache stopped responding, I think because of a memory problem because of so many large arrays. I had problems on the 4th or 5th merge.

What is the best way to merge them and avoid errors?

Update:

[Sat Sep 15 10:17:36 2012] [notice] Apache/2.2.21 (Win32) PHP/5.3.9 configured -- resuming normal operations
[Sat Sep 15 10:17:36 2012] [notice] Server built: Sep 10 2011 11:34:11
[Sat Sep 15 10:17:36 2012] [notice] Parent: Created child process 3908
[Sat Sep 15 10:17:36 2012] [notice] Child 3908: Child process is running
[Sat Sep 15 10:17:36 2012] [notice] Child 3908: Acquired the start mutex.
[Sat Sep 15 10:17:36 2012] [notice] Child 3908: Starting 64 worker threads.
[Sat Sep 15 10:17:36 2012] [notice] Child 3908: Starting thread to listen on port 8080.
[Sat Sep 15 10:17:53 2012] [notice] Parent: child process exited with status 3221225477 -- Restarting.

This is my apache error log. I did find some solutions with copying files to my system32 folder but they didnt work

Upvotes: 0

Views: 2673

Answers (4)

Nuryagdy Mustapayev
Nuryagdy Mustapayev

Reputation: 785

In terms of peak memory usage array_push($a, ...$b) uses least memory in comparison to array_merge($a, $b) and [...$a, ...$b]. Peak Memory usage comparisons:

  1. array_push($a, ...$b) => $peakMemory = $aMemory + $bMemory;
  2. $a = array_merge($a, $b) => $peakMemory = 2*$aMemory + $bMemory;
  3. $a = [...$a, ...$b] => $peakMemory = 2*$aMemory + $bMemory;

Also, in case when you use array_merge or ellipsis operator, to reduce peak memory usage, make sure you assign the return value to the biggest array.

Upvotes: 0

gogic1
gogic1

Reputation: 111

Problem was not in array_merge but in curl that i was doing to external file . File was too big for curl as i see it and then we had memory problems, increasing memory in php.ini didnt fix the problem

Upvotes: 0

Hawili
Hawili

Reputation: 1659

using + is more optimised then using array_merge

Write it like this, it is faster and use less resources, which seems you have problem with

$arrayTotal = $array1 + $array2 + $array3 + $array4; //add as much as you need :)

note:thanks to @ficuscr, from php array_merge page, a real important thing to watch is

If you want to append array elements from the second array to the first array while not overwriting the elements from the first array and not re-indexing, use the + array union operator

in short words, if you JUST want to append things, without caring for duplicates, use +

Upvotes: 2

Niet the Dark Absol
Niet the Dark Absol

Reputation: 324840

array_merge can take an unlimited number of arguments, so you can put them all in one function call:

$arrayTotal = array_merge($array1,$array2,$array3...);

This should solve the problem, since I've used arrays with thousands of items in the past with no issues.

Upvotes: 9

Related Questions