1

I have these values:

$x[0][0] = 1;
$x[0][1] = 2;
$x[0][2] = 3;

$x[1][0] = 4;
$x[1][1] = 5;
$x[1][2] = 6;

$x[2][0] = 7;
$x[2][1] = 8;
$x[2][2] = 9;

Now, i need to make an autonomous array. Basically, it will change with the dimension. For example a 4*4 and not a 3*3.

    $arr= array(
    0=>array($x[0][0],$x[0][1],$x[0][2]), 
    1=>array($x[1][0],$x[1][1],$x[1][2]), 
    2=>array($x[2][0],$x[2][1],$x[2][2])
    )

I am trying a for loop, but without success. Any idea?

0

2 Answers 2

1
$x[0][0] = 1;
$x[0][1] = 2;
$x[0][2] = 3;

$x[1][0] = 4;
$x[1][1] = 5;
$x[1][2] = 6;

$x[2][0] = 7;
$x[2][1] = 8;
$x[2][2] = 9;

$arr = array();
for ($i = 0; $i < 3; $i++) {
    $arr[$i] = array();
    for ($j = 0; $j < 3; $j++) {
        $arr[$i][$j] = $x[$i][$j];

    }   
}

print_r($arr);

If I understand you correctly

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

Comments

0

You mean nested loop ? like (sample values ) :

$ar = array();
for( $i =0 ;$i<5;$i++)
{
  for($j=0;$j<5;$j++)
  {
     $ar[$i][$j] = 0 ;
  }

}

1 Comment

sorry i mean $ar[$i][$j] = 0 ;

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.