Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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