Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Multidimensional arrays 1

Status
Not open for further replies.

Masali

Programmer
Joined
Jun 19, 2002
Messages
143
Location
SE
Hi,

If I have an array that I want to push into an array, how do i do? I do not want to merge the arrays to one, i want to put the pointer to the array into an array.
Code:
for ($x=0;$x<10;$x++)
{
$array1=array(&quot;this1&quot; => &quot;one&quot;,&quot;this2&quot; => &quot;two&quot;,&quot;this3&quot; => &quot;three&quot;);
$array2=array();

array_push($array2[$x],$array1);
}
This will not produce a multidimensional array... How can produce so it looks like this..
Code:
$array2[0][&quot;this1&quot;]=&quot;one&quot;;
$array2[3][&quot;this2&quot;]=&quot;two&quot;;
Please help..

Masali
 
Why are you repeatedly redeclaring your arrays inside the for loop?

There is no deterministic way to get the output you want. You're doing some kind of array-index skipping in your example output.

But something like:

Code:
<?php
$array1=array(&quot;this1&quot; => &quot;one&quot;,&quot;this2&quot; => &quot;two&quot;,&quot;this3&quot; => &quot;three&quot;);
$array2=array();
$counter = 0;
foreach ($array1 as $index => $value)
{
        $array2[$counter][$index] = $value;
        $counter++;
}

print_r ($array2);
?>

will output:

Code:
Array
(
    [0] => Array
        (
            [this1] => one
        )

    [1] => Array
        (
            [this2] => two
        )

    [2] => Array
        (
            [this3] => three
        )

)

Want the best answers? Ask the best questions: TANSTAAFL!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top