Jump to content

2 Arrays of Same Name


Ken2k7

Recommended Posts

Okay, I'll make this succinct and straight to the point. These are just examples.

 

l_com.php

<?php

$lancom = array(

'hi'   =>   'hello',
'by'    =>   'bye'

);

?>

 

l_ucp.php

<?php

$lancom = array(

'welcome'    =>    "welcome to %s",
'leaving'      =>    'leaving so soon?'

);

?>

 

 

index.php

<?php

include_once "l_com.php";
include_once "l_ucp.php";

// contents here

?>

 

Question: I plan to use both arrays in index.php, but how would I differentiate between them? Any way I can go about doing this? I'm open to any and all suggestions.

 

Thank you in advance,

Ken

Link to comment
Share on other sites

Index will inherit both arrays, so there will be a collision since they are defined. You have separate associative indexes for them, so both arrays can both be used by calling them separate array names in their respective scripts, then using array_merge to bring them back under one array name. I might recommend:

 

In l_com.php

 

<?php

$lancom = array( 'hi'   =>   'hello',
                 'by'    =>   'bye'
                );

?>

 

 

In l_ucp.php

 

<?php

$lancom2 = array( 'welcome'    =>    "welcome to %s",
                  'leaving'      =>    'leaving so soon?'
                  );

?>

 

index.php

 

<?php

include_once "l_com.php";
include_once "l_ucp.php";

$lancom = array_merge($lancom, $lancom2);

// contents here

?>

 

A print_r($lancom) in index.php after the array merge would produce:

 

Array
(
    [hi] => hello
    [by] => bye
    [welcome] => welcome to %s
    [leaving] => leaving so soon?
)

 

There's probably many other ways to do this, as there are so many powerful array tools. You get the idea tho...

 

PhREEEk

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.