Example 1 — Collect Values With the Same Key
When two arrays share a key, the values are gathered into a list.
<?php
$a = ["a" => "x"];
$b = ["a" => "y"];
$result = array_merge_recursive($a, $b);
echo json_encode($result), PHP_EOL; How It Works
- Create two arrays. Both use the key
a. - Call array_merge_recursive(). It sees the key twice.
- Combine the values. They are placed in a list:
xtheny. - Print as JSON. The key
anow holds an array.

