2

I want to update the following array:

$old_array = array('c' => array( "a" => "1", "b" => "2"))

By adding the following array to it:

$new_array = array('cc' => array( "a" => "1", "b" => "2"))

My expected output is:

$update_array = array('c' => array( "a" => "1", "b" => "2"), 'cc' => array( "a" => "1", "b" => "2"))

How would I go about achieving this? Any help is appreciated.

7
  • 3
    Just use array_merge(); like $update_array = array_merge($old_array, $new_array); Commented Dec 26, 2018 at 11:41
  • as @executable said, check php.net/manual/en/function.array-merge.php Commented Dec 26, 2018 at 11:45
  • Thanks guys, work it. Commented Dec 26, 2018 at 11:46
  • Use array_push() Commented Dec 26, 2018 at 11:47
  • 1
    I'm sorry!now it's ok, Commented Dec 26, 2018 at 12:02

3 Answers 3

2

You will need to merge the array using the function array_merge()

More information about the function here.

$old_array = array('c' => array( "a" => "1", "b" => "2")); 
$new_array = array('cc' => array( "a" => "1", "b" => "2"));

// merge arrays
$merged_array = array_merge($old_array, $new_array);
Sign up to request clarification or add additional context in comments.

2 Comments

Nice! If it solved your issue, please consider upvoting / accepting the answer. It will help other people in the future. (You can accept an answer 15 minutes after the question being asked)
Done! Thanks again.
1

You can use array_merge

$old_array = array('c' => array( "a" => "1", "b" => "2"));
$new_array = array('cc' => array( "a" => "1", "b" => "2"));

$result = array_merge($old_array, $new_array);

Comments

1

You can do that with array_merge() :

$old_array = array('c' => array( "a" => "1", "b" => "2")) ;
$new_array = array('cc' => array( "a" => "1", "b" => "2"));
$update_array = array_merge($old_array, $new_array); 

Output:

array:2 [▼
  "c" => array:2 [▼
    "a" => "1"
    "b" => "2"
  ]
  "cc" => array:2 [▼
    "a" => "1"
    "b" => "2"
  ]
]

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.