 <pre><code>&lt;?php $classreport = array( array( 'name' =&gt; 'Vick', 'std' =&gt; 'A', 'marks' =&gt; array(10, 20, 30, 40) ), array( 'name' =&gt; 'Josh', 'std' =&gt; 'B', 'marks' =&gt; array(20, 40) ), ); $csv = 'name, std, mark'."\r\n"; foreach($classreport as $student) { $markrows = 0; foreach ($student['marks'] as $marks) { if ($markrows == 0) { $csv .= sprintf('%s, %s, %d', $student['name'], $student['std'], $marks)."\r\n"; } else { $csv .= sprintf(', , %d', $marks)."\r\n"; $markrows++; } } } ?&gt; </code></pre> <p>Edit: Actually, I just realized it's easier to just add these things to arrays and then use the <code>csv</code> functions from PHP. Just consider this as pseudo-code to help you on your way.</p>