tags:

views:

51

answers:

3

Hello!

I have an array:

print_r($resultArray);

Array
(
    [AB34] => Array
        (
            [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 ...
+1  A: 

$resArray = $resultArray;

... also use print_r($var, TRUE); in order to get the full contents of the variable.

Narf
1. some explanation never hurts // 2. this is one failure in the code of Tim, but dosn't tell why he's only getting a "1"
oezi
I updated my first post. I have to put several $resultArray's into $resArray, so I will need the index.
Tim
Err, http://www.php.net/print_r – the second parameter tells it to return output of the function instead of printing it.
Matěj Grabovský
+1  A: 

Try without brackets like this:

$resArray = $resultArray;
print_r($resArray);
Sarfraz
I updated my first post. I have to put several $resultArray's into $resArray, so I will need the index.
Tim
+1  A: 

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
                )

        )

)
Michael Clerx