Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

array_merge not working as expected

I have an double level array. On the first level there are about 10 indexes. These contain each to a 275 element array which each contains a word.

Array
(
[0] => Array
    (
    [0] => Suspendisse
    [1] => Nam.
    [2] => Amet
    [3] => amet
    [4] => urna
    [5] => condimentum
    [6] => Vestibulum
    [7] => sem
    [8] => at
    [9] => Curabitur
    [10] => lorem
    .... to [275]
    )
[1] => Array
    (
    ... you get the idea
    )
... 10 elements total
)

Now, through circumstances, like an added image that takes up space, I sometimes need to recalculate the number of words that are remaining and redistribute the array that still remains.

For that I have written the below function to make the remaining array one big long array of words, that I can array_chunk then to the right proportions. &$array is the reference to the "mother array", $memory is an array of words that are surplus, $index is where we are iterating through a for loop, $limit is "array" length for the 2nd level, in this case 275

function redistribute(&$array,$memory,$index,$limit)
{
$ret[] = $array[0];
// skip past the current processed items
for($c=1;$c<$index;$c++)
    {
    $ret[] = $array[$c];
    }
// apply current item
$ret[] = $array[$index];

//join the rest into one big array off words;
$r2=$memory;
$length = count($array);
for($c=$index+1;$c<$length;++$c)
    {
    array_merge($r2,$array[$c]);
    print_r($r2);
    }
}

The question

array_merge(arr1,arr2) doesn't seem to work.

when executing

redistribute($splitupchunk,array('test','test2','test3','test4'),$i,275);

print_r($r2) just prints test numbers without the extra variables from $array[$c]. How do I fix this?

like image 294
Tschallacka Avatar asked Jul 02 '12 08:07

Tschallacka


People also ask

What does the array_merge () function do?

The array_merge() function merges one or more arrays into one array. Tip: You can assign one array to the function, or as many as you like. Note: If two or more array elements have the same key, the last one overrides the others.

How to merge array values in php?

The array_merge() is a builtin function in PHP and is used to merge two or more arrays into a single array. This function is used to merge the elements or values of two or more arrays together into a single array.

What is the difference between array_merge () and Array_merge_recursive () in PHP?

The array_merge_recursive() function merges one or more arrays into one array. The difference between this function and the array_merge() function is when two or more array elements have the same key. Instead of override the keys, the array_merge_recursive() function makes the value as an array.


1 Answers

I found the reason why it didn't work. I assumed that array_merge worked by reference instead of by return.

By making the line

array_merge($r2,$array[$c]);

$r2 = array_merge($r2,$array[$c]);

It now works as expected.

like image 159
Tschallacka Avatar answered Sep 28 '22 03:09

Tschallacka