views:

3942

answers:

5

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']
A: 

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)?

Seb
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
+5  A: 

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'
Pim Jager
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
+1  A: 

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>';
Peter Bailey
Until you assign to `$b[1]`, in your example, `$a` and `$b` are not separate copies.
grantwparks
+1  A: 

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.

Ilya Birman
following statement is really helpful to me.$tempParams = array ();
Syed Tayyab Ali
+1  A: 

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.

KOGI
thank you buddy.my problem has solved now...
Syed Tayyab Ali
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