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.
The CSV is composed by this structure
USER,PRINTER,PAGES,COPIES,GRAYSCALE,DUPLEX
001,001,1,2,G,D
001,002,1,3,C,ND
002,003,2,2,C,D
So far I've managed to create a PHP script that outputs the main Array with the "users" sub-array inside.
Something like this
Array
(
['user1']=>Array
(
['printer1']=>Array
(
[PAGES]=>'1'
)
)
)
This for multiple users, but i can't seem to multiply the printers inside the Users array. This is my code:
<?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' => $data[2]];
}
/*$arrprint ["'".$data[1]."'"] = [ "'".$data[4]."'" => ['NÂșPag' => $data[2]]];*/
$arrprint["'".$data[1]."'"] = $arrprinter;
}
print_r ($arrprint);
fclose($handle);
}
What Am I doing wrong? Is there any loop missing from my code?
EDIT: As Requested The desired output would be:
array(
['user1']=>array(
['printer1']=>Array( 'bw_cp'=10 'colour'=20)
['printer2']=>Array( 'bw_cp'=5 'colour_cp'=1 )
)
)
This would be the final idea!
["'".$data[1]."'"]
this type of thing is quite wonky, you don't need anything but[$data[1]]
. – phpisuber01 Apr 9 '13 at 11:50