array_merge vs just doing it [message #170287] |
Tue, 26 October 2010 23:13 |
Brian Smither
Messages: 5 Registered: October 2010
Karma: 0
|
Junior Member |
|
|
I've studied array_merge and I know what it does regarding disparate
key/value pairs.
But what is the "strategy" of using array_merge when just assigning
key/values to the array would work just as well?
Ex.1:
// Existing array
$lang['front']['area1']="This is prime.";
$lang['front']['area2']="This is sub-prime.";
// New elements
$modd['front']['loc3']="This is new.";
$modd['front']['loc4']="This is new, too.";
// merge the two
$lang = array_merge($lang,$modd);
Ex.2:
// Existing array
$lang['front']['area1']="This is prime.";
$lang['front']['area2']="This is sub-prime.";
// New elements
$lang['front']['loc3']="This is new.";
$lang['front']['loc4']="This is new, too.";
// it just is
|
|
|
Re: array_merge vs just doing it [message #170288 is a reply to message #170287] |
Wed, 27 October 2010 01:17 |
Jerry Stuckle
Messages: 2598 Registered: September 2010
Karma: 0
|
Senior Member |
|
|
On 10/26/2010 7:13 PM, Brian Smither wrote:
> I've studied array_merge and I know what it does regarding disparate
> key/value pairs.
>
> But what is the "strategy" of using array_merge when just assigning
> key/values to the array would work just as well?
>
> Ex.1:
>
> // Existing array
> $lang['front']['area1']="This is prime.";
> $lang['front']['area2']="This is sub-prime.";
>
> // New elements
> $modd['front']['loc3']="This is new.";
> $modd['front']['loc4']="This is new, too.";
>
> // merge the two
> $lang = array_merge($lang,$modd);
>
>
> Ex.2:
>
> // Existing array
> $lang['front']['area1']="This is prime.";
> $lang['front']['area2']="This is sub-prime.";
>
> // New elements
> $lang['front']['loc3']="This is new.";
> $lang['front']['loc4']="This is new, too.";
>
> // it just is
>
>
>
>
array_merge() is used when you have two different arrays containing data
already, i.e. from function calls, not when you're generating an array.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
|
|
|
Re: array_merge vs just doing it [message #170296 is a reply to message #170287] |
Wed, 27 October 2010 10:14 |
alvaro.NOSPAMTHANX
Messages: 277 Registered: September 2010
Karma: 0
|
Senior Member |
|
|
El 27/10/2010 1:13, Brian Smither escribió/wrote:
> I've studied array_merge and I know what it does regarding disparate
> key/value pairs.
>
> But what is the "strategy" of using array_merge when just assigning
> key/values to the array would work just as well?
Almost all native functions can be replaced with ad-hoc alternatives
coded in plain PHP. Functions basically exist for your convenience so
you don't have to waste time reinventing the wheel and pasting the same
blocks of code again and again. In these case, I find it useful to be
able to merge arrays without the need of writing nested foreach loops
every time.
--
-- http://alvaro.es - Álvaro G. Vicario - Burgos, Spain
-- Mi sitio sobre programación web: http://borrame.com
-- Mi web de humor satinado: http://www.demogracia.com
--
|
|
|