1

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

5 Answers 5

5

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' 
Sign up to request clarification or add additional context in comments.

1 Comment

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...
1

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>';

1 Comment

Until you assign to $b[1], in your example, $a and $b are not separate copies.
1

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.

1 Comment

following statement is really helpful to me. $tempParams = array ();
1

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.

1 Comment

Arrays are not copied during assignment according to my research, until one or the other variable is updated. It's called "copy on write".
0

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

1 Comment

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.