1

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

3 Answers 3

1

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
                )

        )

)
Sign up to request clarification or add additional context in comments.

Comments

1

$resArray = $resultArray;

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

3 Comments

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"
I updated my first post. I have to put several $resultArray's into $resArray, so I will need the index.
Err, php.net/print_r – the second parameter tells it to return output of the function instead of printing it.
1

Try without brackets like this:

$resArray = $resultArray;
print_r($resArray);

1 Comment

I updated my first post. I have to put several $resultArray's into $resArray, so I will need the index.

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.