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 recursively2$array1 = [3'colors' => [4'red' => '#ff0000',5'green' => '#00ff00'6],7'sizes' => ['small', 'medium']8];910$array2 = [11'colors' => [12'blue' => '#0000ff',13'green' => '#00cc00' // This will override the green in array114],15'sizes' => ['large'] // This will be merged with sizes in array116];1718$merged = cbo_array_merge_recursive($array1, $array2);1920// Result:21// [22// 'colors' => [23// 'red' => '#ff0000',24// 'green' => '#00cc00', // Value from array2 overrides array125// 'blue' => '#0000ff'26// ],27// 'sizes' => ['small', 'medium', 'large'] // Values are merged28// ]
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_recursive2$array1 = ['key' => 'value1'];3$array2 = ['key' => 'value2'];45$result1 = array_merge_recursive($array1, $array2);6// Result: ['key' => ['value1', 'value2']]78// cbo_array_merge_recursive9$result2 = cbo_array_merge_recursive($array1, $array2);10// Result: ['key' => 'value2']
Usage Examples
Merging Configuration Arrays
1// Merge default configuration with user configuration2function get_merged_config() {3// Default configuration4$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' => false16]17],18'performance' => [19'cache' => true,20'minify' => [21'css' => true,22'js' => true,23'html' => false24]25]26];2728// User configuration29$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' => true38]39],40'performance' => [41'minify' => [42'css' => false43]44]45];4647// Merge configurations48return cbo_array_merge_recursive($default_config, $user_config);49}5051// Usage52$config = get_merged_config();53echo 'Site Title: ' . $config['site']['title'] . '<br>';54echo 'Cache Enabled: ' . ($config['performance']['cache'] ? 'Yes' : 'No') . '<br>';55echo 'Facebook Enabled: ' . ($config['features']['social']['facebook'] ? 'Yes' : 'No') . '<br>';
Merging Theme Options
1// Merge theme options with defaults2function get_theme_options() {3// Default theme options4$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];2324// Get saved options from database25$saved_options = get_option('theme_options', []);2627// Merge with defaults28return cbo_array_merge_recursive($defaults, $saved_options);29}3031// Usage32$options = get_theme_options();33echo 'Primary Color: ' . $options['colors']['primary'] . '<br>';34echo 'Body Font: ' . $options['typography']['body_font'] . '<br>';35echo '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.