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 8839115
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T10:10:04+00:00 2026-06-14T10:10:04+00:00

I have a csv file which I am trying to turn into a different

  • 0

I have a csv file which I am trying to turn into a different structured array. First, I turn it into an array named all_data() constructed like this:

$data = file_get_contents($id . '.csv');
      $data_array = explode("\n", $data);
      foreach($data_array AS $data){
            $all_data[] = explode("\t", $data);
      }

results look like this:

    array(5) {
      [0]=>
      array(2) {
        [0]=>
        string(10) "2012-11-14"
        [1]=>
        string(2) "10"
      }
      [1]=>
      array(2) {
        [0]=>
        string(10) "2012-11-14"
        [1]=>
        string(2) "10"
      }
      [2]=>
      array(2) {
        [0]=>
        string(10) "2012-11-14"
        [1]=>
        string(2) "10"
      }
      [3]=>
      array(2) {
        [0]=>
        string(10) "2012-11-14"
        [1]=>
        string(2) "10"
      }

      [4]=>
      array(1) {
        [0]=>
        string(0) ""
      }

}

And then I turn it into im_arr() with the following code:

  foreach($all_data as $key => $value){
            $im_arr[$key][$value[0]] = $value[1];
       }

The results:

array(5) {
  [0]=>
  array(1) {
    ["2012-11-14"]=>
    string(2) "10"
  }
  [1]=>
  array(1) {
    ["2012-11-14"]=>
    string(2) "10"
  }
  [2]=>
  array(1) {
    ["2012-11-14"]=>
    string(2) "10"
  }
  [3]=>
  array(1) {
    ["2012-11-14"]=>
    string(2) "10"
  }

  [4]=>
  array(1) {
    [""]=>
    NULL
  }
}

And then, finally another foreach loop gives me the results I am looking for:

foreach ($im_arr as $val) {
    foreach ($val as $key => $val2) {
        $im_data[$key]=$val2;
    }
       }

With the result for im_data() being:

array(2) {
  ["2012-11-14"]=>
  string(2) "10"
  [""]=>
  NULL
}

Which would be perfect, since the array im_data() is exactly what I would like to get out of all_data(). However, when I am trying to put this code in another part of the program it doesn’t work, and I am thinking it might be because of the warnings I receive:

“PHP Notice: Undefined offset: 1 in … on line 93”

Line 93 corresponds to this line:

$im_arr[$key][$value[0]] = $value[1];

Here is the complete part of the code:

  $all_data = array();
  $im_arr=array();

$data = file_get_contents($id . '.csv');
      $data_array = explode("\n", $data);
      foreach($data_array AS $data){
            $all_data[] = explode("\t", $data);
      }

      foreach($all_data as $key => $value){
            $im_arr[$key][$value[0]] = $value[1];  //the line for the error
       }
    $im_data=array();  

foreach ($im_arr as $val) {
    foreach ($val as $key => $val2) {
        $im_data[$key]=$val2;
    }
       }




var_dump($im_data);

I know there are many many questions posted for this same error, but I couldn’t figure out the problem with this particular piece of code.

  • 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-06-14T10:10:05+00:00Added an answer on June 14, 2026 at 10:10 am

    This is the problem:

    [4]=>
      array(1) {
        [0]=>
        string(0) ""
      }
    

    Just check that the data is set, and isn’t empty before adding them to $im_arr:

    foreach ($all_data as $key => $value) { 
      if (isset($value[0]) && isset($value[1]) && !empty($value[0]) && !empty($value[1])) {
        $im_arr[$key][$value[0]] = $value[1];
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a csv file from which I have to populate different tables in
I have a CSV file the first row of which contains the variables names
I have a csv file which has 3 columns. I am trying to search
I have a csv file which is something like this: book_store_id book_price name 1
I have a CSV file which contains around 1200 rows. I was trying to
This is what I am trying to achieve: I have a CSV file which
I am trying to import a .csv file into a table. I have figured
1) I'm trying to create CSV file using NSMutableArray(array of array) which I want
I have a rather large csv file (17GB) which I'm trying to sanity check.
Little Background: I have csv file which has lots of rows and each row

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.