PHP - copy an array does not work
I have an array:
print_r($resultArray);
Array
(
[AB34] => Array
开发者_如何学Go(
[a] => 13
[b] => 10
[c] => 3
[d] => 88
[e] => 73
)
...
)
And I want to copy this array into another one:
$resArray[] = $resultArray;
print_r($resArray);
->
Array
(
[0] => 1
)
So the new array $resArray does not have the content of $resultArray. What needed to be done to solve this?
Best Regards.
UPDATE: I have to copy the $resultArray into $resArray (that's an easy example), because $resultArray will change and I need the data in a $resArray with index, so $resArray[0] the first $resultArray, $resArray[1] the second full value of the $resultArray, ... Some code (only a simple example!):
$resArray[0] = $resultArray;
... calculations on $resultArray ...
$resArray[1] = $resultArray;
... calculations on $resultArray ...
$resArray[2] = $resultArray;
... calculations on $resultArray ...
I can only guess you have a small syntax error somewhere. My testcase works as expected:
$resultArray = array(
'AB34' => array(
'a' => 13,
'b' => 10,
'c' => 3,
'd' => 88,
'e' => 73
)
);
echo '<pre>';
echo "Printing \$resultArray\n";
print_r($resultArray);
$resArray[] = $resultArray;
$resArray[] = $resultArray;
$resArray[0]['AB34']['c'] = 'Penguins are neat';
echo "\n\nPrinting \$resArray\n";
print_r($resArray);
Returns
Printing $resultArray
Array
(
[AB34] => Array
(
[a] => 13
[b] => 10
[c] => 3
[d] => 88
[e] => 73
)
)
Printing $resArray
Array
(
[0] => Array
(
[AB34] => Array
(
[a] => 13
[b] => 10
[c] => Penguins are neat
[d] => 88
[e] => 73
)
)
[1] => Array
(
[AB34] => Array
(
[a] => 13
[b] => 10
[c] => 3
[d] => 88
[e] => 73
)
)
)
$resArray = $resultArray;
... also use print_r($var, TRUE);
in order to get the full contents of the variable.
Try without brackets like this:
$resArray = $resultArray;
print_r($resArray);
精彩评论