Note that there are some explanatory texts on larger screens.

plurals
  1. POArray with multiple Arrays from CSV PHP
    text
    copied!<p>I'm trying to learn PHP and so developing a PHP script that will read a CSV file and place it in a array with multiple arrays inside.</p> <p>The CSV is composed by this structure</p> <pre><code>USER,PRINTER,PAGES,COPIES,GRAYSCALE,DUPLEX 001,001,1,2,G,D 001,002,1,3,C,ND 002,003,2,2,C,D </code></pre> <p>So far I've managed to create a PHP script that outputs the main Array with the "users" sub-array inside.</p> <p>Something like this</p> <pre><code>Array ( ['user1']=&gt;Array ( ['printer1']=&gt;Array ( [PAGES]=&gt;'1' ) ) ) </code></pre> <p>This for multiple users, but i can't seem to multiply the printers inside the Users array. This is my code:</p> <pre><code>&lt;?php $arrprint = array(); $arrprinter = array(); if (($handle = fopen("log-2013-04-03.csv", "r")) !== FALSE) { while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) { if (! isset($arrprint ["".$data[1].""]) ){ $arrprint ["'".$data[1]."'"] = $data[1]; if(! isset($arrprinter["".$data[4].""]) ) $arrprinter["'".$data[4]."'"] = ['Pages' =&gt; $data[2]]; } /*$arrprint ["'".$data[1]."'"] = [ "'".$data[4]."'" =&gt; ['NºPag' =&gt; $data[2]]];*/ $arrprint["'".$data[1]."'"] = $arrprinter; } print_r ($arrprint); fclose($handle); } </code></pre> <p>What Am I doing wrong? Is there any loop missing from my code?</p> <p>EDIT: As Requested The desired output would be: </p> <pre><code>array( ['user1']=&gt;array( ['printer1']=&gt;Array( 'bw_cp'=10 'colour'=20) ['printer2']=&gt;Array( 'bw_cp'=5 'colour_cp'=1 ) ) ) </code></pre> <p>This would be the final idea!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload