up vote 1 down vote favorite

I have a variable $params which gets data from the database:

$params = mssql_fetch_array($result)

As far as I know, it is associative array. I want another array $tempParams to hold the value of this array. Can I assign it by using the following statement:

$tempParams = $params

In addition, do I need one single statement to declare and assign a value to $tempParams, or can these be separated?

One more question I would like to ask is that following statement is correct; While $tempParams contains values;

$params['column1'] = $tempParams['newColumns']
link|flag

5 Answers

up vote 5 down vote accepted

Yes,

$tempParams = $params;

Will copy all values from $params to $tempParams.

$params['foo'] = 'bar';
echo $tempParams['foo']; //nothing
$tempParams = $params;
echo $tempParams['foo']; //'bar'
$params['foo'] = 'hai';
echo $tempParams['foo']; //still: 'bar'
link|flag
thank you man, your reply is worked for me. I even dont have the comiler php. But just follow all the reply and correct the logic... – Syed Tayyab Ali Mar 31 '09 at 18:35
up vote 1 down vote

As far as whether or not your array is associative, read the documentation on mysql_fetch_array()

As far as assignment goes, you actually can put it in one statement

$tempParams = $params = mysql_fetch_array( $result, MYSQL_ASSOC );

This simple test shows that when you do an assignment like this, both variables are separate copies and not references.

$a = $b = array( 1, 2, 3 );

$b[1] = 'x';

echo '<pre>';
print_r( $a );
print_r( $b );
echo '</pre>';
link|flag
Until you assign to $b[1], in your example, $a and $b are not separate copies. – grantwparks Sep 25 '09 at 3:10
up vote 1 down vote

Yes, the = operator will copy the array exactly.

You can check yourself:

// get the $params from DB
print_r ($params); // will output array contents
$tempParams = $params;
print_r ($tempParams); // must be the same as above

There’s no such thing as “declaring” variables in PHP, but if you wish to say that $tempParams is an array somewhere before assigning, you can do it like this:

$tempParams = array ();

This will make $tempParams an array with no elements inside.

link|flag
following statement is really helpful to me. $tempParams = array (); – Syed Tayyab Ali Apr 1 '09 at 2:21
up vote 1 down vote

For arrays, numeric and associative, the = operator will make a copy of the variable. And both variables are completely independent of one another. However, when dealing with objects, the = operator creates a reference to the object, and both variables point to the exact same object.

link|flag
thank you buddy. my problem has solved now... – Syed Tayyab Ali Apr 1 '09 at 2:18
Arrays are not copied during assignment according to my research, until one or the other variable is updated. It's called "copy on write". – grantwparks Sep 25 '09 at 3:07
up vote 0 down vote

Yes you can, but that could cause some kind of aliasing if you're dealing with Objects (depending on which PHP version you're using).

Why is it that you want to copy the array? Can't you work with the same original variable ($params)?

link|flag
I want to ignore some columns from database, because somebody has added new columns. While I want to retail old values of table, just in case if user access any old record.. – Syed Tayyab Ali Mar 31 '09 at 18:05

Your Answer

get an OpenID
or
never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.