{"id":1309,"date":"2016-06-14T02:40:25","date_gmt":"2016-06-14T02:40:25","guid":{"rendered":"https:\/\/www.homehost.com.br\/blog\/?p=1309"},"modified":"2023-06-08T09:22:30","modified_gmt":"2023-06-08T12:22:30","slug":"notice-undefined-offset-php","status":"publish","type":"post","link":"https:\/\/www.homehost.com.br\/blog\/uncategorized\/notice-undefined-offset-php\/","title":{"rendered":"Solucionando o erro Notice: Undefined offset do PHP"},"content":{"rendered":"<p>Um erro muito comum quando mexemos com vetores (arrays) no PHP, \u00e9 o Notice: Undefined offset. Ele significa que estamos tentando realizar uma opera\u00e7\u00e3o com um array, buscando por um \u00edtem inexistente. A mensagem de erro que normalmente surge \u00e9 similar a esta:<\/p>\n<pre class=\"lang:default decode:true \">Notice: Undefined offset: 5 in \/home\/meusitemodelo\/public_html\/teste.php on line 5<\/pre>\n<p><img fetchpriority=\"high\" decoding=\"async\" class=\"alignnone size-full wp-image-1311\" style=\"border: 1px solid gray;\" src=\"https:\/\/www.homehost.com.br\/blog\/wp-content\/uploads\/2016\/06\/Notice-Undefined-Offset-PHP.jpg\" alt=\"Notice Undefined Offset PHP\" width=\"605\" height=\"269\" srcset=\"https:\/\/www.homehost.com.br\/blog\/wp-content\/uploads\/2016\/06\/Notice-Undefined-Offset-PHP.jpg 605w, https:\/\/www.homehost.com.br\/blog\/wp-content\/uploads\/2016\/06\/Notice-Undefined-Offset-PHP-300x133.jpg 300w, https:\/\/www.homehost.com.br\/blog\/wp-content\/uploads\/2016\/06\/Notice-Undefined-Offset-PHP-1x1.jpg 1w\" sizes=\"(max-width: 605px) 100vw, 605px\" \/><\/p>\n<p>Um exemplo de c\u00f3digo fonte em PHP que ir\u00e1 gerar este erro:<\/p>\n<pre class=\"lang:default decode:true\">&lt;?php\r\n\r\n$estados = array(\"SP\",\"PB\",\"RS\");\r\n\r\necho $estados[5];\r\n\r\n?&gt;<\/pre>\n<p>Este erro ocorre pois o array\u00a0$estados , conforme est\u00e1 sendo definido, ter\u00e1 os seguintes valores:<\/p>\n<p>$estados[0] = &#8220;SP&#8221;;<br \/>\n$estados[1] = &#8220;PB&#8221;;<br \/>\n$estados[2] = &#8220;RS&#8221;;<\/p>\n<p>Como estamos tentando chamar a posi\u00e7\u00e3o 5 do array, por ela ser inexistente, causar\u00e1 o <a href=\"https:\/\/www.homehost.com.br\/blog\/tutoriais\/php\/notice-undefined-index-php\/\" >erro Notice: Undefined offset<\/a>.<\/p>\n<h3>Solu\u00e7\u00e3o 1: Usar o sizeof() para percorrer o array<\/h3>\n<p>Este erro pode normalmente ser corrigido implementando o sizeof(). Esta fun\u00e7\u00e3o retorna o n\u00famero de registros em um array:<\/p>\n<pre class=\"lang:default decode:true\">&lt;?php\r\n\r\n$estados = array(\"SP\",\"PB\",\"RS\");\r\n\r\necho sizeof($estados);\r\n\r\n?&gt;<\/pre>\n<p>Este c\u00f3digo ir\u00e1 nos retornar o resultado 3. Desse modo, o array\u00a0$estados, possui 3 itens.<\/p>\n<p>Importante: o sizeof() sempre nos retornar\u00e1 uma posi\u00e7\u00e3o a mais em rela\u00e7\u00e3o ao \u00edndice do array, pois o vetor come\u00e7a sempre com 0.<\/p>\n<p>A forma correta de se percorrer um vetor:<\/p>\n<pre class=\"lang:default decode:true\">&lt;?php\r\n\r\n$estados = array(\"SP\",\"PB\",\"RS\");\r\n\r\nfor ($i=0;$i&lt;sizeof($estados);$i++)\r\n{\r\n\u00a0 \u00a0 \u00a0 \u00a0 echo \"$i - $estados[$i] &lt;br&gt;\";\r\n}\r\n\r\n?&gt;<\/pre>\n<p>Este trecho de c\u00f3digo ir\u00e1 exibir:<\/p>\n<p>0 &#8211; SP<br \/>\n1 &#8211; PB<br \/>\n2 &#8211; RS<\/p>\n<h3>Solu\u00e7\u00e3o 2: Usar o print_r() como debug para imprimir o array completo<\/h3>\n<p>O comando print_r() \u00e9 muito \u00fatil para visualizar de forma completa um array. Igualmente, quando estamos trabalhando com arrays multidimensionais.<\/p>\n<p>Neste exemplo, vamos popular um array multidimensional:<\/p>\n<pre class=\"lang:default decode:true\">$clientes[0]['nome']=\"Joao\";\r\n$clientes[0]['cidade']=\"Sao Paulo\";\r\n\r\n$clientes[1]['nome']=\"Fabio\";\r\n$clientes[1]['cidade']=\"Belo Horizonte\";\r\n\r\n$clientes[2]['nome']=\"Jair\";\r\n$clientes[2]['cidade']=\"Macapa\";\r\n<\/pre>\n<p>Em seguida, vamos incluir no c\u00f3digo-fonte o comando print_r:<\/p>\n<pre class=\"lang:default decode:true\">print_r($clientes);\r\n<\/pre>\n<p>O resultado, ser\u00e1 toda a estrutura do nosso array de forma organizada:<\/p>\n<pre class=\"lang:default decode:true\">Array\r\n(\r\n   [0] =&gt; Array\r\n   (\r\n      [nome] =&gt; Joao\r\n      [cidade] =&gt; Sao Paulo\r\n   )\r\n\r\n   [1] =&gt; Array\r\n   (\r\n      [nome] =&gt; Fabio\r\n      [cidade] =&gt; Belo Horizonte\r\n   )\r\n\r\n   [2] =&gt; Array\r\n   (\r\n      [nome] =&gt; Jair\r\n      [cidade] =&gt; Macapa\r\n   )\r\n\r\n)\r\n<\/pre>\n<p>Dessa forma \u00e9 poss\u00edvel entender exatamente a estrutura do array, e realizar a chamada da forma correta.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Um erro muito comum quando mexemos com vetores (arrays) no PHP, \u00e9 o Notice: Undefined offset. Ele significa que estamos tentando realizar uma opera\u00e7\u00e3o com um array, buscando por um \u00edtem inexistente. A mensagem de erro que normalmente surge \u00e9 similar a esta: Notice: Undefined offset: 5 in \/home\/meusitemodelo\/public_html\/teste.php on line 5 Um exemplo de [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_helpful_status":1,"_lmt_disableupdate":"","_lmt_disable":"","_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[229,1],"tags":[193,21],"class_list":["post-1309","post","type-post","status-publish","format-standard","hentry","category-outros","category-uncategorized","tag-notice-undefined-offset","tag-php"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.6 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Solucionando o erro Notice: Undefined offset do PHP | Homehost<\/title>\n<meta name=\"description\" content=\"Artigo ensinando a corrigir o problema de Notice: Undefined offset: 5 in \/home\/meusitemodelo\/public_html\/teste.php on line 5\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.homehost.com.br\/blog\/uncategorized\/notice-undefined-offset-php\/\" \/>\n<meta property=\"og:locale\" content=\"pt_BR\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Solucionando o erro Notice: Undefined offset do PHP | Homehost\" \/>\n<meta property=\"og:description\" content=\"Artigo ensinando a corrigir o problema de Notice: Undefined offset: 5 in \/home\/meusitemodelo\/public_html\/teste.php on line 5\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.homehost.com.br\/blog\/uncategorized\/notice-undefined-offset-php\/\" \/>\n<meta property=\"og:site_name\" content=\"Homehost\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/Homehost\/\" \/>\n<meta property=\"article:published_time\" content=\"2016-06-14T02:40:25+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-06-08T12:22:30+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.homehost.com.br\/blog\/wp-content\/uploads\/2016\/06\/Notice-Undefined-Offset-PHP.jpg\" \/>\n<meta name=\"author\" content=\"Gustavo Gallas\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@Homehost\" \/>\n<meta name=\"twitter:site\" content=\"@Homehost\" \/>\n<meta name=\"twitter:label1\" content=\"Escrito por\" \/>\n\t<meta name=\"twitter:data1\" content=\"Gustavo Gallas\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. tempo de leitura\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutos\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.homehost.com.br\/blog\/uncategorized\/notice-undefined-offset-php\/\",\"url\":\"https:\/\/www.homehost.com.br\/blog\/uncategorized\/notice-undefined-offset-php\/\",\"name\":\"Solucionando o erro Notice: Undefined offset do PHP | Homehost\",\"isPartOf\":{\"@id\":\"https:\/\/www.homehost.com.br\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.homehost.com.br\/blog\/uncategorized\/notice-undefined-offset-php\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/www.homehost.com.br\/blog\/uncategorized\/notice-undefined-offset-php\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.homehost.com.br\/blog\/wp-content\/uploads\/2016\/06\/Notice-Undefined-Offset-PHP.jpg\",\"datePublished\":\"2016-06-14T02:40:25+00:00\",\"dateModified\":\"2023-06-08T12:22:30+00:00\",\"author\":{\"@id\":\"https:\/\/www.homehost.com.br\/blog\/#\/schema\/person\/df8002f43fc55e7f4e132abb2a6ddbc4\"},\"description\":\"Artigo ensinando a corrigir o problema de Notice: Undefined offset: 5 in \/home\/meusitemodelo\/public_html\/teste.php on line 5\",\"breadcrumb\":{\"@id\":\"https:\/\/www.homehost.com.br\/blog\/uncategorized\/notice-undefined-offset-php\/#breadcrumb\"},\"inLanguage\":\"pt-BR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.homehost.com.br\/blog\/uncategorized\/notice-undefined-offset-php\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"pt-BR\",\"@id\":\"https:\/\/www.homehost.com.br\/blog\/uncategorized\/notice-undefined-offset-php\/#primaryimage\",\"url\":\"https:\/\/www.homehost.com.br\/blog\/wp-content\/uploads\/2016\/06\/Notice-Undefined-Offset-PHP.jpg\",\"contentUrl\":\"https:\/\/www.homehost.com.br\/blog\/wp-content\/uploads\/2016\/06\/Notice-Undefined-Offset-PHP.jpg\",\"width\":605,\"height\":269},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.homehost.com.br\/blog\/uncategorized\/notice-undefined-offset-php\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"In\u00edcio\",\"item\":\"https:\/\/www.homehost.com.br\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Solucionando o erro Notice: Undefined offset do PHP\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.homehost.com.br\/blog\/#website\",\"url\":\"https:\/\/www.homehost.com.br\/blog\/\",\"name\":\"Homehost\",\"description\":\"Hospedagem De Sites\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.homehost.com.br\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"pt-BR\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.homehost.com.br\/blog\/#\/schema\/person\/df8002f43fc55e7f4e132abb2a6ddbc4\",\"name\":\"Gustavo Gallas\",\"description\":\"Analista de sistemas, formado pela PUC-Rio. Programador, gestor de redes e diretor da empresa Homehost. Pai do B\u00f3ris, seu pet de estima\u00e7\u00e3o. Gosta de rock'n'roll, cerveja artesanal e de escrever sobre assuntos t\u00e9cnicos. Contato: gustavo.blog@homehost.com.br\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Solucionando o erro Notice: Undefined offset do PHP | Homehost","description":"Artigo ensinando a corrigir o problema de Notice: Undefined offset: 5 in \/home\/meusitemodelo\/public_html\/teste.php on line 5","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.homehost.com.br\/blog\/uncategorized\/notice-undefined-offset-php\/","og_locale":"pt_BR","og_type":"article","og_title":"Solucionando o erro Notice: Undefined offset do PHP | Homehost","og_description":"Artigo ensinando a corrigir o problema de Notice: Undefined offset: 5 in \/home\/meusitemodelo\/public_html\/teste.php on line 5","og_url":"https:\/\/www.homehost.com.br\/blog\/uncategorized\/notice-undefined-offset-php\/","og_site_name":"Homehost","article_publisher":"https:\/\/www.facebook.com\/Homehost\/","article_published_time":"2016-06-14T02:40:25+00:00","article_modified_time":"2023-06-08T12:22:30+00:00","og_image":[{"url":"https:\/\/www.homehost.com.br\/blog\/wp-content\/uploads\/2016\/06\/Notice-Undefined-Offset-PHP.jpg","type":"","width":"","height":""}],"author":"Gustavo Gallas","twitter_card":"summary_large_image","twitter_creator":"@Homehost","twitter_site":"@Homehost","twitter_misc":{"Escrito por":"Gustavo Gallas","Est. tempo de leitura":"2 minutos"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.homehost.com.br\/blog\/uncategorized\/notice-undefined-offset-php\/","url":"https:\/\/www.homehost.com.br\/blog\/uncategorized\/notice-undefined-offset-php\/","name":"Solucionando o erro Notice: Undefined offset do PHP | Homehost","isPartOf":{"@id":"https:\/\/www.homehost.com.br\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.homehost.com.br\/blog\/uncategorized\/notice-undefined-offset-php\/#primaryimage"},"image":{"@id":"https:\/\/www.homehost.com.br\/blog\/uncategorized\/notice-undefined-offset-php\/#primaryimage"},"thumbnailUrl":"https:\/\/www.homehost.com.br\/blog\/wp-content\/uploads\/2016\/06\/Notice-Undefined-Offset-PHP.jpg","datePublished":"2016-06-14T02:40:25+00:00","dateModified":"2023-06-08T12:22:30+00:00","author":{"@id":"https:\/\/www.homehost.com.br\/blog\/#\/schema\/person\/df8002f43fc55e7f4e132abb2a6ddbc4"},"description":"Artigo ensinando a corrigir o problema de Notice: Undefined offset: 5 in \/home\/meusitemodelo\/public_html\/teste.php on line 5","breadcrumb":{"@id":"https:\/\/www.homehost.com.br\/blog\/uncategorized\/notice-undefined-offset-php\/#breadcrumb"},"inLanguage":"pt-BR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.homehost.com.br\/blog\/uncategorized\/notice-undefined-offset-php\/"]}]},{"@type":"ImageObject","inLanguage":"pt-BR","@id":"https:\/\/www.homehost.com.br\/blog\/uncategorized\/notice-undefined-offset-php\/#primaryimage","url":"https:\/\/www.homehost.com.br\/blog\/wp-content\/uploads\/2016\/06\/Notice-Undefined-Offset-PHP.jpg","contentUrl":"https:\/\/www.homehost.com.br\/blog\/wp-content\/uploads\/2016\/06\/Notice-Undefined-Offset-PHP.jpg","width":605,"height":269},{"@type":"BreadcrumbList","@id":"https:\/\/www.homehost.com.br\/blog\/uncategorized\/notice-undefined-offset-php\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"In\u00edcio","item":"https:\/\/www.homehost.com.br\/blog\/"},{"@type":"ListItem","position":2,"name":"Solucionando o erro Notice: Undefined offset do PHP"}]},{"@type":"WebSite","@id":"https:\/\/www.homehost.com.br\/blog\/#website","url":"https:\/\/www.homehost.com.br\/blog\/","name":"Homehost","description":"Hospedagem De Sites","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.homehost.com.br\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"pt-BR"},{"@type":"Person","@id":"https:\/\/www.homehost.com.br\/blog\/#\/schema\/person\/df8002f43fc55e7f4e132abb2a6ddbc4","name":"Gustavo Gallas","description":"Analista de sistemas, formado pela PUC-Rio. Programador, gestor de redes e diretor da empresa Homehost. Pai do B\u00f3ris, seu pet de estima\u00e7\u00e3o. Gosta de rock'n'roll, cerveja artesanal e de escrever sobre assuntos t\u00e9cnicos. Contato: gustavo.blog@homehost.com.br"}]}},"modified_by":"Gustavo Gallas","jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.homehost.com.br\/blog\/wp-json\/wp\/v2\/posts\/1309","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.homehost.com.br\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.homehost.com.br\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.homehost.com.br\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.homehost.com.br\/blog\/wp-json\/wp\/v2\/comments?post=1309"}],"version-history":[{"count":6,"href":"https:\/\/www.homehost.com.br\/blog\/wp-json\/wp\/v2\/posts\/1309\/revisions"}],"predecessor-version":[{"id":5337,"href":"https:\/\/www.homehost.com.br\/blog\/wp-json\/wp\/v2\/posts\/1309\/revisions\/5337"}],"wp:attachment":[{"href":"https:\/\/www.homehost.com.br\/blog\/wp-json\/wp\/v2\/media?parent=1309"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.homehost.com.br\/blog\/wp-json\/wp\/v2\/categories?post=1309"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.homehost.com.br\/blog\/wp-json\/wp\/v2\/tags?post=1309"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}