1

I am having trouble generating a multidimensional array from a csv file. I need to have the output 'grouped' by country as each country may have multiple networks. Some rows do not have a value for country or zone as they are related to the row above it. Unfortunately this is how i receive the csv file and there is no way of changing the output. Any feedback or pointers would be appreciated.

Snippet of csv file...

Country|Zone|Network|Video|Voice
Afghanistan,5,Afghan Wireless,No,Yes
,,Roshan,No,Yes
Antigua,4,Digicel,No,Yes
Argentina,5,Telecom Personal,Yes,Yes
,,Movistar,No,Yes
,,Movistar2,Yes,Yes
Aruba,4,Digicel,No

Ideal Output

Array (
    [0] => Array (
        [country] => Afghanistan
        [zone] => 5
        [network] => Array (
            [0] => Array (
                [name] => Afghan Wireless
                [video] => No
                [voice] => Yes
            )
            [1] => Array (
                [name] => Roshan
                [video] => No
                [voice] => Yes
            )
        )
    )
    [1] => Array (
        [country] => Antigua
        [zone] => 4
        [network] => Array (
            [0] => Array (
                [name] => Digicell
                [video] => No
                [voice] => Yes
            )
        )
    )
    etc...
)

2 Answers 2

2
<?php 

$csvArray=array();//to store final data 
$networkArray=array();//to serve as temporar buffer

if (($handle = fopen("file.csv", "r")) !== FALSE) 
{
     fgetcsv($handle, 1000, ",");//skip the first line cause contains labels only
    //iterate all ther line 
    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) 
    { 
        //if a new country
        if($data[0]!=='')
        {  
            /*get last key assigned to the previous country*/
            $key=array_pop(array_keys($csvArray)); 
            /*store the buffer, at the very begining no last country exists 
            so this network will be stored in a null key, will delete it later*/
            $csvArray[$key]['network']=$networkArray; 
            //emty the buffer
            $networkArray=array(); 
            //now we are done with previous country and will store the new one
            $csvArray[]=Array('country'=>$data[0],'zone'=>$data[1]);        
        }//@if ends 
            //Put to buffer network, video and voice
            $networkArray[]=Array('name'=>$data[2],'video'=>$data[3],'voice'=>$data[4]);     
    }//@while ends
    fclose($handle);
}//@outer if ends

//store the last network buffer
$key=array_pop(array_keys($csvArray));  
$csvArray[$key]['network']=$networkArray;   
//delete the null key set in the begining
array_shift($csvArray); 

//show the array
echo '<pre>'; 
print_r($csvArray); 

?>
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much, worked a treat. And i also learned a bit more about array and loops.
0

Just a simple solution (not e complete tested one)

<?php

// Initialize the final result
$result = array();

// Read the file line by line
$handle = @fopen("test.csv", "r");
if ($handle) {
    $country = null;

    while (($buffer = fgets($handle, 4096)) !== false) {

        $line = explode(',', $buffer);

        if($line[0] == null) {

            $network['name'] = $line[2];
            $network['video'] = $line[3];
            if(isset($line[4])) $network['voice'] = $line[4];

            $country['network'][] = $network;

            $network = null;

        } else {
            if($country != null) {
                $result[] = $country;
                $country = null;
            }

            $country['country'] = $line[0];
            $country['zone'] = $line[1];

            $network['name'] = $line[2];
            $network['video'] = $line[3];
            if(isset($line[4])) $network['voice'] = $line[4];

            $country['network'][] = $network;

            $network = null;

        }

    }
    if (!feof($handle)) {
        echo "Error: unexpected fgets() fail\n";
    }
    fclose($handle);

    print_r($result);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.