array_merge vs the + operator

Login

Blog History


<?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.