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.

6
  • Create the subarray before adding keys to it. if(!array_key_exists($key, $im_arr)) { $im_arr[$key] = array(); } . Or what MarcB said, some of your values are not in correct format. Commented Nov 15, 2012 at 15:16
  • 1
    Probably there's at least one line in your file that doesn't have a \t, giving you at least one entry in $all_data that doesn't have a 1 index. Commented Nov 15, 2012 at 15:16
  • That makes sense, but I still get the same error notification. Commented Nov 15, 2012 at 15:19
  • Marc B: Could that be why I get the last array element with "" => NULL? How would I go about solving that? Commented Nov 15, 2012 at 15:20
  • Yeah that's the problem, just check that they're set before processing them: if (isset($value[0]) && isset($value[1])) { process data! } Commented Nov 15, 2012 at 15:21

2 Answers 2

2

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

For every foreach i would pre-check if the first argument is an array

For instance ;

//Just add line below for every foreach (and add any required else statement if needed)
if(is_array($im_arr))
foreach ($im_arr as $val) {
    if(is_array($val))
    foreach ($val as $key => $val2) {
        $im_data[$key]=$val2;
    }
}

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.