0

I was wondering if it was possible to merge two arrays and then override the property of the original array.

$originalArray = [1,2,3];
$newArray = [
         'first'  => array_merge($originalArray, [4,5]),
         'second' => array_merge($originalArray, [6,7]),
];

So what I want to achieve is $originalArray would be [1,2,3], then $newArray['first'] would be [1,2,3,4,5] and then $newArray['second'] would be [1,2,3,4,5,6,7]

Right now, what I have is $originalArray is [1,2,3], then $newArray['first'] is [1,2,3,4,5] and then $newArray['second'] is [1,2,3,6,7] and that makes perfect sense as $originalArray is being merged on both first and second instance but $originalArray is not being overwritten.

I was wondering if that is possible at all? Can we intermediately override the values of $originalArray within $newArray?

3
  • why don't you perform array_merge before assigning it into $newArray? Commented Jan 16, 2020 at 5:38
  • @AnkitSingh the code above is just an excerpt of what I am working on. So ideally, I dont know if thats possible, but I was thinking if it was possible to do it within $newArray itself. Commented Jan 16, 2020 at 5:39
  • you want your originalArray values to be same with newArray at the end right ? Commented Jan 16, 2020 at 5:47

5 Answers 5

4

Use additional variables to hold the accumulated mergers.

$originalArray = [1,2,3];

$newArray = [
    'first' => ($tempArray = array_merge($originalArray, [4,5])),
    'second' => ($tempArray = array_merge($tempArray, [6,7])),
    'third' => ($tempArray = array_merge($tempArray, [8,9])),
    ...
];
Sign up to request clarification or add additional context in comments.

6 Comments

Thanks for your reply. I was wondering if it is possible to override the value from within the array itself
Why is that important? Isn't the result what matters?
Yes, but the code logic that I have is much complex, and I will have to create multiple intermediate variables which I want to avoid.
I've updated the answer to show how to put the variable assignment inside the array.
If you have lots of them, it might be easier to write a function that uses a loop.
|
3

Merge as you go with a loop, and update your original array with the last merge.

<?php

$orig  = [1,2,3];
$trans = [
         'first'  => [4,5],
         'second' => [6,7],
];

foreach($trans as &$v) {
    $orig = $v = array_merge($orig, $v);
}
unset($v);

var_export($trans);

Output:

array (
  'first' => 
  array (
    0 => 1,
    1 => 2,
    2 => 3,
    3 => 4,
    4 => 5,
  ),
  'second' => 
  array (
    0 => 1,
    1 => 2,
    2 => 3,
    3 => 4,
    4 => 5,
    5 => 6,
    6 => 7,
  ),
)

1 Comment

This answer seems the most elegant and direct to me. This is what I'd use in a professional script. It is the least verbose answer which dynamically accumulates data with each iteration.
2

Here is what you want to do:

$originalArray = [1,2,3];
$newArray = [
         'first'  => array_merge($originalArray, [4,5]),
         'second' => array_merge($originalArray, [6,7]),
];


function val($newArray) { return $newArray; }
$originalArray = array_map( 'val' , $newArray);
print_r( $originalArray );

Output:

Array ( 
[first] => Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 ) 
[second] => Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 6 [4] => 7 ) 
)

1 Comment

This unexplained answer does not provide the desired result. This provides the same result as the asker's code.
1

below will help

$originalArray = [1,2,3];


$firstArr = ['first'=> array_merge($originalArray, [4,5])];
$secArr = ['second'=> array_merge($firstArr['first'], [6,7])];
print_r($secArr);
//OR
$newArray = [
         'first'  => array_merge($originalArray, [4,5]),
         'second' => array_merge(array_merge($originalArray,[4,5]), [6,7]),
];
print_r($newArray['second']);

1 Comment

This hardcoded approach is not helpful for the task. The whole point is to dynamically accumulate values as you iterate.
1

Try the below code. It's working for you.

<?php
$originalArray = [1,2,3];

$newArray['first'] = array_merge($originalArray, [4,5]);
$newArray['second'] = array_merge($newArray['first'], [6,7]);
$newArray['third'] = array_merge($newArray['second'], [8,9]);

echo "<pre>";
print_r($newArray);
?>

=> Output

Array
(
    [first] => Array
        (
            [0] => 1
            [1] => 2
            [2] => 3
            [3] => 4
            [4] => 5
        )

    [second] => Array
        (
            [0] => 1
            [1] => 2
            [2] => 3
            [3] => 4
            [4] => 5
            [5] => 6
            [6] => 7
        )

    [third] => Array
        (
            [0] => 1
            [1] => 2
            [2] => 3
            [3] => 4
            [4] => 5
            [5] => 6
            [6] => 7
            [7] => 8
            [8] => 9
        )

)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.