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
https://forums.phpfreaks.com/topic/83207-2-arrays-of-same-name/
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
https://forums.phpfreaks.com/topic/83207-2-arrays-of-same-name/#findComment-423316
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.