Ken2k7 Posted December 26, 2007 Share Posted December 26, 2007 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 More sharing options...
jitesh Posted December 26, 2007 Share Posted December 26, 2007 The array of l_ucp.php will be overwrite on l_com.php. You can use 2 dimensional array. $lancom = array( 'l_com' => array( 'hi' => 'hello', 'by' => 'bye') , 'l_ucp' => array( 'welcome' => 'welcome to %s', 'leaving' => 'leaving so soon?') ) Link to comment https://forums.phpfreaks.com/topic/83207-2-arrays-of-same-name/#findComment-423315 Share on other sites More sharing options...
PHP_PhREEEk Posted December 26, 2007 Share Posted December 26, 2007 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 More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.