array_merge vs the + operator

by | May 27, 2022 | IT Tips | 0 comments

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<?php
 
$options = [
    'rootNode' => 'response'
];
 
// array_merge
// the same key appearing later will overwrite
echo print_r(array_merge(['rootNode' => 'root'], $options), true);
/**
 * output
 *
 * Array
 * (
 *     [rootNode] => response
 * )
 */
 
// + syntax if it already exists + will NOT overwrite
echo print_r(['rootNode' => 'root'] + $options, true);
 
/**
 * Output
 *
 * Array
 * (
 *     [rootNode] => root
 * )
 **/

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

The reCAPTCHA verification period has expired. Please reload the page.