Arrays

The arrays utility function provides a tool for merging arrays recursively with improved handling compared to PHP's native array_merge_recursive function.

Available Function

cbo_array_merge_recursive(array $array1, array $array2, array...$arrays)

Merges arrays recursively with improved handling compared to PHP's native array_merge_recursive function.

Parameters:

  • $array1: The base array.
  • $array2: The array to merge with the base array.
  • $arrays: Additional arrays to merge.

Return Value:

Returns the merged array.

1
// Merge two arrays recursively
2
$array1 = [
3
'colors' => [
4
'red' => '#ff0000',
5
'green' => '#00ff00'
6
],
7
'sizes' => ['small', 'medium']
8
];
9
10
$array2 = [
11
'colors' => [
12
'blue' => '#0000ff',
13
'green' => '#00cc00' // This will override the green in array1
14
],
15
'sizes' => ['large'] // This will be merged with sizes in array1
16
];
17
18
$merged = cbo_array_merge_recursive($array1, $array2);
19
20
// Result:
21
// [
22
// 'colors' => [
23
// 'red' => '#ff0000',
24
// 'green' => '#00cc00', // Value from array2 overrides array1
25
// 'blue' => '#0000ff'
26
// ],
27
// 'sizes' => ['small', 'medium', 'large'] // Values are merged
28
// ]

Comparison with PHP's array_merge_recursive

The main difference between cbo_array_merge_recursive and PHP's native array_merge_recursive is how they handle string keys with the same name:

  • array_merge_recursive: Creates an array of values when keys are the same.
  • cbo_array_merge_recursive: Overwrites the value with the one from the second array.
1
// PHP's array_merge_recursive
2
$array1 = ['key' => 'value1'];
3
$array2 = ['key' => 'value2'];
4
5
$result1 = array_merge_recursive($array1, $array2);
6
// Result: ['key' => ['value1', 'value2']]
7
8
// cbo_array_merge_recursive
9
$result2 = cbo_array_merge_recursive($array1, $array2);
10
// Result: ['key' => 'value2']

Usage Examples

Merging Configuration Arrays

1
// Merge default configuration with user configuration
2
function get_merged_config() {
3
// Default configuration
4
$default_config = [
5
'site' => [
6
'title' => 'Default Site Title',
7
'description' => 'Default site description',
8
'logo' => '/images/default-logo.png'
9
],
10
'features' => [
11
'comments' => true,
12
'social' => [
13
'facebook' => true,
14
'twitter' => true,
15
'instagram' => false
16
]
17
],
18
'performance' => [
19
'cache' => true,
20
'minify' => [
21
'css' => true,
22
'js' => true,
23
'html' => false
24
]
25
]
26
];
27
28
// User configuration
29
$user_config = [
30
'site' => [
31
'title' => 'My Custom Site',
32
'logo' => '/images/custom-logo.png'
33
],
34
'features' => [
35
'social' => [
36
'facebook' => false,
37
'instagram' => true
38
]
39
],
40
'performance' => [
41
'minify' => [
42
'css' => false
43
]
44
]
45
];
46
47
// Merge configurations
48
return cbo_array_merge_recursive($default_config, $user_config);
49
}
50
51
// Usage
52
$config = get_merged_config();
53
echo 'Site Title: ' . $config['site']['title'] . '<br>';
54
echo 'Cache Enabled: ' . ($config['performance']['cache'] ? 'Yes' : 'No') . '<br>';
55
echo 'Facebook Enabled: ' . ($config['features']['social']['facebook'] ? 'Yes' : 'No') . '<br>';

Merging Theme Options

1
// Merge theme options with defaults
2
function get_theme_options() {
3
// Default theme options
4
$defaults = [
5
'colors' => [
6
'primary' => '#0073aa',
7
'secondary' => '#23282d',
8
'background' => '#ffffff',
9
'text' => '#333333'
10
],
11
'typography' => [
12
'body_font' => 'Arial, sans-serif',
13
'heading_font' => 'Georgia, serif',
14
'base_size' => '16px',
15
'line_height' => '1.5'
16
],
17
'layout' => [
18
'container_width' => '1200px',
19
'sidebar' => 'right',
20
'header_style' => 'default'
21
]
22
];
23
24
// Get saved options from database
25
$saved_options = get_option('theme_options', []);
26
27
// Merge with defaults
28
return cbo_array_merge_recursive($defaults, $saved_options);
29
}
30
31
// Usage
32
$options = get_theme_options();
33
echo 'Primary Color: ' . $options['colors']['primary'] . '<br>';
34
echo 'Body Font: ' . $options['typography']['body_font'] . '<br>';
35
echo 'Container Width: ' . $options['layout']['container_width'] . '<br>';

Best Practices

  • Use cbo_array_merge_recursive instead of PHP's native array_merge_recursive when you want to override values rather than create arrays of values.
  • Use for configuration arrays where you want to merge default settings with user settings.
  • Be aware of the behavior with numeric keys, which will be reindexed just like with PHP's array_merge.
  • Consider performance when merging large arrays, as recursive operations can be resource-intensive.