Merging Multi-Demensional Arrays
Posted: Tue Nov 28, 2006 4:53 pm
As you can see I'm trying to merge two arrays on the first level key. The problem is the values of the second array are not merged as I thought it would.
Burrito mentioned array_merge() doesn't play well with multi-dimensional arrays, so I came up with a quick hack 
This is obviously very ugly, any thoughts of an easier way? I know I must be complicating things. One of those days...
Code: Select all
$errors['field'] = array();
$errors['field'][] = 'blah';
$errors['field'][] = 'blah2';
$errors['field'][] = 'blah3';
$errors2['field'] = array();
$errors2['field'][] = 'blah33';
$errors2['field'][] = 'blah23';
$errors2['field'][] = 'blah33';
$newerror = array_merge($errors, $errors2);Code: Select all
Array
(
[field] => Array
(
[0] => blah
[1] => blah2
[2] => blah3
)
)Code: Select all
$field = array_keys($errors2);
foreach ($errors2[$field[0]] as $error)
{
array_push($errors[$field[0]], $error);
}