Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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!

share|improve this question
1  
You should provide the desired output as you envision it for the example input. As it is this does not make sense. –  Jon Apr 9 '13 at 11:48
    
["'".$data[1]."'"] this type of thing is quite wonky, you don't need anything but [$data[1]]. –  phpisuber01 Apr 9 '13 at 11:50
    
Agree with Jon. please provide desired output –  yasir hashmi Apr 9 '13 at 11:50
    
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 –  Pedro Oliveira Apr 9 '13 at 13:05

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.