{"id":1651,"date":"2022-08-30T15:18:18","date_gmt":"2022-08-30T15:18:18","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/27\/php-error-undefined-offset-error-within-foreach-loop-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:18:18","modified_gmt":"2022-08-30T15:18:18","slug":"php-error-undefined-offset-error-within-foreach-loop-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/php-error-undefined-offset-error-within-foreach-loop-collection-of-common-programming-errors\/","title":{"rendered":"PHP Error: Undefined offset error within foreach loop-Collection of common programming errors"},"content":{"rendered":"<p>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:<\/p>\n<pre><code>$data = file_get_contents($id . '.csv');\n      $data_array = explode(\"\\n\", $data);\n      foreach($data_array AS $data){\n            $all_data[] = explode(\"\\t\", $data);\n      }\n<\/code><\/pre>\n<p>results look like this:<\/p>\n<pre><code>    array(5) {\n      [0]=&gt;\n      array(2) {\n        [0]=&gt;\n        string(10) \"2012-11-14\"\n        [1]=&gt;\n        string(2) \"10\"\n      }\n      [1]=&gt;\n      array(2) {\n        [0]=&gt;\n        string(10) \"2012-11-14\"\n        [1]=&gt;\n        string(2) \"10\"\n      }\n      [2]=&gt;\n      array(2) {\n        [0]=&gt;\n        string(10) \"2012-11-14\"\n        [1]=&gt;\n        string(2) \"10\"\n      }\n      [3]=&gt;\n      array(2) {\n        [0]=&gt;\n        string(10) \"2012-11-14\"\n        [1]=&gt;\n        string(2) \"10\"\n      }\n\n      [4]=&gt;\n      array(1) {\n        [0]=&gt;\n        string(0) \"\"\n      }\n\n}\n<\/code><\/pre>\n<p>And then I turn it into im_arr() with the following code:<\/p>\n<pre><code>  foreach($all_data as $key =&gt; $value){\n            $im_arr[$key][$value[0]] = $value[1];\n       }\n<\/code><\/pre>\n<p>The results:<\/p>\n<pre><code>array(5) {\n  [0]=&gt;\n  array(1) {\n    [\"2012-11-14\"]=&gt;\n    string(2) \"10\"\n  }\n  [1]=&gt;\n  array(1) {\n    [\"2012-11-14\"]=&gt;\n    string(2) \"10\"\n  }\n  [2]=&gt;\n  array(1) {\n    [\"2012-11-14\"]=&gt;\n    string(2) \"10\"\n  }\n  [3]=&gt;\n  array(1) {\n    [\"2012-11-14\"]=&gt;\n    string(2) \"10\"\n  }\n\n  [4]=&gt;\n  array(1) {\n    [\"\"]=&gt;\n    NULL\n  }\n}\n<\/code><\/pre>\n<p>And then, finally another foreach loop gives me the results I am looking for:<\/p>\n<pre><code>foreach ($im_arr as $val) {\n    foreach ($val as $key =&gt; $val2) {\n        $im_data[$key]=$val2;\n    }\n       }\n<\/code><\/pre>\n<p>With the result for im_data() being:<\/p>\n<pre><code>array(2) {\n  [\"2012-11-14\"]=&gt;\n  string(2) \"10\"\n  [\"\"]=&gt;\n  NULL\n}\n<\/code><\/pre>\n<p>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&#8217;t work, and I am thinking it might be because of the warnings I receive:<\/p>\n<p>&#8220;PHP Notice: Undefined offset: 1 in &#8230; on line 93&#8221;<\/p>\n<p>Line 93 corresponds to this line:<\/p>\n<pre><code>$im_arr[$key][$value[0]] = $value[1];\n<\/code><\/pre>\n<p>Here is the complete part of the code:<\/p>\n<pre><code>  $all_data = array();\n  $im_arr=array();\n\n$data = file_get_contents($id . '.csv');\n      $data_array = explode(\"\\n\", $data);\n      foreach($data_array AS $data){\n            $all_data[] = explode(\"\\t\", $data);\n      }\n\n      foreach($all_data as $key =&gt; $value){\n            $im_arr[$key][$value[0]] = $value[1];  \/\/the line for the error\n       }\n    $im_data=array();  \n\nforeach ($im_arr as $val) {\n    foreach ($val as $key =&gt; $val2) {\n        $im_data[$key]=$val2;\n    }\n       }\n\n\n\n\nvar_dump($im_data);\n<\/code><\/pre>\n<p>I know there are many many questions posted for this same error, but I couldn&#8217;t figure out the problem with this particular piece of code.<\/p>\n<ol>\n<li>\n<p>This is the problem:<\/p>\n<pre><code>[4]=&gt;\n  array(1) {\n    [0]=&gt;\n    string(0) \"\"\n  }\n<\/code><\/pre>\n<p>Just check that the data is set, and isn&#8217;t empty before adding them to <code>$im_arr<\/code>:<\/p>\n<pre><code>foreach ($all_data as $key =&gt; $value) { \n  if (isset($value[0]) &amp;&amp; isset($value[1]) &amp;&amp; !empty($value[0]) &amp;&amp; !empty($value[1])) {\n    $im_arr[$key][$value[0]] = $value[1];\n  }\n}\n<\/code><\/pre>\n<\/li>\n<li>\n<p>For every foreach i would pre-check if the first argument is an array<\/p>\n<p>For instance ;<\/p>\n<pre><code>\/\/Just add line below for every foreach (and add any required else statement if needed)\nif(is_array($im_arr))\nforeach ($im_arr as $val) {\n    if(is_array($val))\n    foreach ($val as $key =&gt; $val2) {\n        $im_data[$key]=$val2;\n    }\n}\n<\/code><\/pre>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2013-11-27 12:24:51. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>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 . &#8216;.csv&#8217;); $data_array = explode(&#8220;\\n&#8221;, $data); foreach($data_array AS $data){ $all_data[] = explode(&#8220;\\t&#8221;, $data); } results look like this: array(5) { [0]=&gt; array(2) { [0]=&gt; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1651","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1651","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=1651"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1651\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=1651"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=1651"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=1651"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}