I'm currently cleaning up a bit of nasty code. This code defines to an existing array several new keys in quite the depth:
$form['long_key_name_right_here_you_see']['title'] = 'Hello';
$form['long_key_name_right_here_you_see']['body'] = 'This be a greeting';
$form['long_key_name_right_here_you_see']['etc'] = 'This does nothing';
$form['another_long_key_name_right']['title'] = 'Hello!';
$form['another_long_key_name_right']['body'] = 'This be a greeting!';
$form['another_long_key_name_right']['etc'] = 'This does nothing.';
I would prefer to define this as such:
$arr_form = array('long_key_name_right_here_you_see' => array('title' => 'Hello',
'body' => 'This be a greeting',
'etc' => 'This does nothing'
),
'another_long_key_name_right' => array('title' => 'Hello!',
'body' => 'This be a greeting!',
'etc' => 'This does nothing.'));
$form = ($arr_form + $form);
- Can I always count on my prefered approach to do the same result as existing case?
- Would my prefered approach be considered unclear and pointless?
Essentially, I just feel that huge walls of identical keys defined in rows looks a bit ugly in the code, but maybe that's just my mind that's picky.
The + operator returns the right-hand array appended to the left-hand array; for keys that exist in both arrays, the elements from the left-hand array will be used, and the matching elements from the right-hand array will be ignored.
Note:
The accepted answer helped me in my case. This question doesn't necessarily have one true answer but might inspire others and as it did me .