Efficiently assigning array var to another var

Discussion in 'PHP' started by SGBoise, Apr 8, 2008.

  1. #1
    Hello,

    I am just curious if this is the most efficient way of assigning an array to another variable.

    Here is my situation. I have an object with a variable $currentlang. I have several files that contain the $lang variable with an array of different languages.

    my code:

    require_once('english.php');
    this->currentlang = $lang;

    my main concern is I don't want two arrays holding the same data since the array can be very large.

    Thanks guys.
     
    SGBoise, Apr 8, 2008 IP
  2. m0nkeymafia

    m0nkeymafia Well-Known Member

    Messages:
    399
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    125
    #2
    to "delete" or clear the memory from the old array simply assign "nothing" to it
    i.e. $array = "";

    The main question is why do you need to assign an array to another variable, and then remove the original. It makes no sense?
     
    m0nkeymafia, Apr 9, 2008 IP
  3. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #3
    I'm pretty sure that php5 only passes a reference to the original array.

    Basically:
    
    
    $array = array{1,2,3,4,5);
    
    $newArray = $array; //you only reference $array, you don't actually create another array
    
    
    PHP:
    If you're using php4 you can reference the array by using &.

    
    
    $array = array{1,2,3,4,5);
    
    $newArray = &$array; 
    
    
    PHP:
     
    jestep, Apr 9, 2008 IP
  4. SGBoise

    SGBoise Peon

    Messages:
    647
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #4
    I want all my functions to go through tms_core class for ease of use and security reasons. I don't think there is a way to load the contents of english.php straight into $this->$currentlang without having to declare an array in the language files. I would prefer it that way or if there is a way to just use one variable.

    My Language file contains:
    <?
    $Lang = array(
    	'welcome' => 'welcome',
    	'video' => 'video',
    );?>
    Code (markup):
    My class file cotains:
    class tms_core extends Smarty {
    
    	//Array of current language
    	var $currentlang;
    
    	function tms_core(){
    		require_once("english.php"); // this is dynamic. it could be french.php.
    		$this->$currentlang = $lang;		
    	}
    
    }
    Code (markup):
     
    SGBoise, Apr 9, 2008 IP
  5. joebert

    joebert Well-Known Member

    Messages:
    2,150
    Likes Received:
    88
    Best Answers:
    0
    Trophy Points:
    145
    #5
    <?
    echo sprintf('%s<br/>', memory_get_usage(true));
    $array = array(); $i = 100000;while($i > 0){$array[]=$i;$i--;}
    echo sprintf('$array = array(); $i = 100000;while($i > 0){$array[]=$i;$i--;}: %s<br/>', memory_get_usage(true));
    $newArray = $array;
    echo sprintf('$newArray = $array;: %s<br/>', memory_get_usage(true));
    $byref = &$array;
    echo sprintf('$byref = &$array; %s<br/>', memory_get_usage(true));
    echo phpversion();
    ?>
    Code (markup):
    262144
    $array = array(); $i = 100000;while($i > 0){$array[]=$i;$i--;}: [U]7602176[/U]
    $newArray = $array;: [U]7602176[/U]
    $byref = &$array; [U]12320768[/U]
    5.2.3
    Code (markup):
     
    joebert, Apr 10, 2008 IP