Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6645173
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T00:15:14+00:00 2026-05-26T00:15:14+00:00

I am having trouble generating a multidimensional array from a csv file. I need

  • 0

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
                 => No
                [voice] => Yes
            )
            [1] => Array (
                [name] => Roshan
                 => No
                [voice] => Yes
            )
        )
    )
    [1] => Array (
        [country] => Antigua
        [zone] => 4
        [network] => Array (
            [0] => Array (
                [name] => Digicell
                 => No
                [voice] => Yes
            )
        )
    )
    etc...
)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T00:15:14+00:00Added an answer on May 26, 2026 at 12:15 am
    <?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); 
    
    ?>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having trouble generating a timestamp in a Windows batch file, because I get
I'm having trouble on generating a report in MySQL. I have this table: CREATE
I having trouble in dividing the HTML frames. I have been using the following
I am having trouble with IE7. I have a header, which is an IMG.
I'm having trouble with global variables in php. I have a $screen var set
Problem: I am having trouble generating an xpath expression that selects a node in
I am having some trouble generating a web form using fields specified in an
I am having trouble with Subsonic 3.0 generating an object for a table which
Having trouble with an HQL query. If I remove the avg(..) from it it
im having trouble extracting the trend name and search query from the json response

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.