Jump to content

Help with Multidimensional Arrays


phoenicoperu

Recommended Posts

This is what i got:

 

$marray = array();

for ($i=0;$i<3;$i++) {
for ($j=3;$j>0;$j--) {
	$marray[] = array( $i => array ($j) );
}
}

echo "<pre>";
print_r ($marray);
echo "</pre>";

 

 

OUTPUT:

Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [0] => 3
                )

        )

    [1] => Array
        (
            [0] => Array
                (
                    [0] => 2
                )

        )

    [2] => Array
        (
            [0] => Array
                (
                    [0] => 1
                )

        )

    [3] => Array
        (
            [1] => Array
                (
                    [0] => 3
                )

        )

    [4] => Array
        (
            [1] => Array
                (
                    [0] => 2
                )

        )

    [5] => Array
        (
            [1] => Array
                (
                    [0] => 1
                )

        )

    [6] => Array
        (
            [2] => Array
                (
                    [0] => 3
                )

        )

    [7] => Array
        (
            [2] => Array
                (
                    [0] => 2
                )

        )

    [8] => Array
        (
            [2] => Array
                (
                    [0] => 1
                )

        )

)

But i would like to have as output something like:

Array
(
    [0] => Array
        (
            [1] => 3 
            [2] => 2
            [3] => 1
        )
    [1] => Array
        (
            [1] => 3 
            [2] => 2
            [3] => 1
        )
.........................etc
)

 

How can i accomplish this using for or while loops?

Thanks in advance.

 

 

Use CODE tags next time.

Link to comment
https://forums.phpfreaks.com/topic/243721-help-with-multidimensional-arrays/
Share on other sites

Two ways for a quick fix:

 

$marray = array();

for ($i=0;$i<3;$i++) {
   $marray[$i] = array();
   for ($j=3;$j>0;$j--) {
      $marray[$i] = array_merge($marray[$i], array($i => $j));
   }
}

 

$marray = array();

for ($i=0;$i<3;$i++) {
   $marray[$i] = array();
   for ($j=3;$j>0;$j--) {
      array_push($marray[$i], $j);
   }
}

Or something easier:

 

foreach(range(0, 2) as $i) {
  $marray[$i] = range(3, 1);
}

This seems like the quickest way to me.

$marray = array();
for ($i=0;$i<3;$i++) {
   $marray[$i] = array();
   for ($j=3;$j>0;$j--) $marray[$i][] = $j;
}

 

Yes.  Same as the array_push() but without the function call.  I still prefer:

 

foreach(range(0, 2) as $i) {
  $marray[$i] = range(3, 1);
}

Or:

 

for($i=0; $i<3; $i++) {
  $marray[] = range(3, 1);
}

Or other variations using $i or not.  If I can think of a way to get rid of the first loop I will post it  ;D

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.