FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » array_merge vs just doing it
Show: Today's Messages :: Polls :: Message Navigator
Switch to threaded view of this topic Create a new topic Submit Reply
array_merge vs just doing it [message #170287] Tue, 26 October 2010 23:13 Go to next message
Brian Smither is currently offline  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 Go to previous messageGo to next message
Jerry Stuckle is currently offline  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 Go to previous message
alvaro.NOSPAMTHANX is currently offline  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
--
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: Php windows system(whois) not working
Next Topic: Study Abroad Scholarship For Study in Australia...
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Wed Dec 04 08:46:25 GMT 2024

Total time taken to generate the page: 0.01888 seconds