note 72023 deleted from function.mysqli-stmt-bind-result by nlopess

From: Date: Thu, 06 Sep 2007 13:29:27 +0000
Subject: note 72023 deleted from function.mysqli-stmt-bind-result by nlopess
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-130246@lists.php.net to get a copy of this message
Note Submitter: Typer85 at gmail dot com ---- Through some testing, I managed to find an answer to my question in the post below this one (Thanks to those that helped out --> you know who you are). "It seems that in PHP when you copy an array with the assignment operator, it does a shallow copy. A shallow copy in simple terms means that references are not dereferenced. The references are copied as well." So basically this means, for any referenced arrays, you can not do this: <?php // Assuming Both Variables Are Arrays. $new = old; ?> Because like pointed out, the references will be copied as well. In the situation of the function that 'matti at withoutthis dot keller dot com' created, look for it 6 posts down, you would need to manually copy the $result array if you fetch your data in a WHILE LOOP, which will always be the case if your result set has more than one row. To create a copy of the $result array, you will need to do a deep copy to force PHP to dereference all elements. In short, it means you have to manually loop through each element and copy it. I wrote a quick function, actually its quite easy, that does this for you and has support for multi-dimensional arrays: <?php function deepCopy( $source ) { // Define Local Variable(s). $final = array( ); // Final Array --> Will House Copied Elements. $i = 0; // Counter For Loop. $sourceCount = count( $source ); // Number Of Elements In Source Array. $sourceKey = array_keys( $source ); // Index Keys For Each Element. /** * Loop Through Each Element. * * Notice that in the loop through each element, instead of using the loop counter as an index for each element, a call to the * variable $sourceKey[ $i ] is made instead. * * This is to offer support for elements that have their index as a string and not an integer. Remember that $sourceKey is an array * that houses the original element index values, whether they are strings or integers. So by using the original index value, any * kind of array will usually work. */ for( $i = 0; $i < $sourceCount; $i++ ) { /** * Recursive Call. * * Sometimes, an element can itself be an Array, thus it too will need to be looped through in order to save all its elements. * A check is made to see if the element is an array. If it is, a recursive call to this function is made with the element as * an argument. */ if( is_array( $source[ $sourceKey[ $i ] ] ) ) { $final[ $sourceKey[ $i ] ] = deepCopy( $source[ $sourceKey[ $i ] ] ); } // Element Is Not An Array --> Save It Normally. else { $final[ $sourceKey[ $i ] ] = $source[ $sourceKey[ $i ] ]; } } // Return Copied Array. return $final; } ?> Your going to need to use this function if you want to copy an array that has referenced elements without the references included. Good Luck, --> Thanks To 'ramirez'

« previous php.notes (#130246) next »