0
$stack = array(1,2,3,4,5,6,7,8,1,2,3,4,5,6,7,8);
$myarr = array(array(),array());
$order = 4;
$i = 1;
$j = 1;

foreach($stack as $value){

    $myarr[$i][$j] = $value;

    if($j % $order == 0){
        $j = 1;
        $i++;
    }else{
         $j++;   
    }


}

echo $myarr[1][2];

for ($i = 1; $i <= $order; $i++){
    for($j = 1; $j <= $order; $j++){

        echo $myarr[$i][$j];
        echo " ";

        if($j % $order == 0){
            echo "<br/>";
        }
    }
}

*I want to convert one dimensional array to two dimensional with the existing element in php. Seems some problem while retrieving element from two 2d array.

2
  • What is the two dimensional array supposed to look like? Commented Apr 22, 2018 at 8:57
  • Assuming you are trying to creating 4*4 multi-dimensional array of $stack. correct?? Commented Apr 22, 2018 at 8:59

1 Answer 1

2

I think you should use array_chunk();

$stack = array(1,2,3,4,5,6,7,8,1,2,3,4,5,6,7,8);

$split = 4;

$array = array_chunk($stack, $split);

print_r($array);

Will output:

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

    [1] => Array
        (
            [0] => 5
            [1] => 6
            [2] => 7
            [3] => 8
        )

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

    [3] => Array
        (
            [0] => 5
            [1] => 6
            [2] => 7
            [3] => 8
        )

)

UPDATED If your goal is to convert an array to a matrix then here is a solution. The solution also has a way to display the matrix as a matrix.

 function arrayToMat($array, $split){

  if(count($array) % $split == 0){

    $row = 1;

    for($i = 0; $i < count($array); $i += $split){

      for($j = 0; $j < $split; $j++){

        $mat[$row][$j+1] = $array[$i+$j];

      }

    $row++;

    }

    return $mat;

  } else {

  return FALSE;

  }

}



function displayMat($mat){

  echo
  '<table>';

  for($i = 1; $i <= count($mat); $i++){

    echo
    '<tr>';

    for($j = 1; $j <= count($mat[$i]); $j++){

      echo
      '<td>' .
      $mat[$i][$j] .
      '</td>';

    }

    echo
    '</tr>';

  }

  echo
  '</table>' . '<br><br>';

}




$stack = array(1,2,3,4,5,6,7,8,1,2,3,4,5,6,7,8);

$split = 4;

$mat = arrayToMat($stack, $split); //Will output your desired matrix indexes starting at 1;
//Will return false if cannot form a matrix.

//Show array structure.  Indexes start at 1;
print_r($mat);

//Prints out as matrix.
displayMat($mat);

//Change up the splits.
$mat = arrayToMat($stack, 2); //8x2 
displayMat($mat);

$mat = arrayToMat($stack, 4); //4x4
displayMat($mat);

$mat = arrayToMat($stack, 6); //Returns False

$mat = arrayToMat($stack, 8); //2x8
displayMat($mat); 

This will output:

//Indexes start at 1.
Array
(
    [1] => Array
        (
            [1] => 1
            [2] => 2
            [3] => 3
            [4] => 4
        )

    [2] => Array
        (
            [1] => 5
            [2] => 6
            [3] => 7
            [4] => 8
        )

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

    [4] => Array
        (
            [1] => 5
            [2] => 6
            [3] => 7
            [4] => 8
        )

)

1   2   3   4
5   6   7   8
1   2   3   4
5   6   7   8

//8x2
1   2
3   4
5   6
7   8
1   2
3   4
5   6
7   8

//4x4  
1   2   3   4
5   6   7   8
1   2   3   4
5   6   7   8

//False with a split of 6

//2x8
1   2   3   4   5   6   7   8
1   2   3   4   5   6   7   8
Sign up to request clarification or add additional context in comments.

3 Comments

But I have to display it in the form of Matrix and and every element should be selected by it's row and column values to that I can make some operation on particular elements.
@Aman Kumar Can you please post in your question an actual example of what it is you need. Displaying something as a matrix is different than handling something as a matrix. This array is essentially a matrix, it holds 4 rows and each row has four columns. Please just post a better example in your question. Thanks
@Aman Kumar Please see updated answer, I think it will sufficiently answer your question. Don't forget to mark as the accepted answer, Cheers!

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.