Dividing numbers from file over array columns
<p><strong>This question is similar to a <a href="http://stackoverflow.com/questions/10420829/read-through-a-file-and-add-numbers-to-array-parts">previous question of mine</a>, but I formulated it wrong. I'm very sorry; here's the actual problem.</strong></p>

<p>I have a file with thousands of numbers underneath each other. Let's simplify by using this:</p>

<pre><code>4
7
1
9
3
3
8
6
2
6
5
1
</code></pre>

<p>What I need is to output a matrix (in the form of an array) with a variable number of matrix-rows. The numbers from the file have to be devided over the rows with the first number going to the first row, the second number to the second row, etc. Or, if you like, the 1st number, and every fourth number after that, go to column 1. The second number, and every fourth number after that, to column 2, etc. In the example below, the number of rows is 3:</p>

<pre><code>array (
  [0] =&gt; 4,9,8,6
  [1] =&gt; 7,3,6,5
  [2] =&gt; 1,3,2,1
)
</code></pre>

<p>And in this example, the number of rows is 4:</p>

<pre><code>array (
  [0] =&gt; 4,3,2
  [1] =&gt; 7,3,6
  [2] =&gt; 1,8,5
  [3] =&gt; 9,6,1
)
</code></pre>

<p>The number of rows is variable.</p>

<p>Currently, with help of <a href="http://stackoverflow.com/users/1178686/oscar-jara">Oscar Jara</a>, I now have this:</p>

<pre><code>$path = "data.csv";
$array = explode("\n", file_get_contents($path));
$numbers = array();
foreach(array_chunk($array, 3) as $number){
  $numbers[] = implode(",", $number);
}
</code></pre>

<p>But this outputs the numbers from the file over rows instead of columns:</p>

<pre><code>array (
  [0] =&gt; 4,7,1
  [1] =&gt; 9,3,3
  [2] =&gt; 8,6,2
  [3] =&gt; 6,5,1
)
</code></pre>

<p>I get confused when transforming this code into dividing into columns. If you don't, then any help is appreciated. </p>
