Home:ALL Converter>How to avoid undefined offset

How to avoid undefined offset

Ask Time:2011-07-05T05:47:01         Author:clarkk

Json Formatter

How can you easily avoid getting this error/notice:

Notice: Undefined offset: 1 in /var/www/page.php on line 149

... in this code:

list($func, $field) = explode('|', $value);

There are not always two values returned by explode, but if you want to use list() how can you then easily avoid the notice?

Author:clarkk,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/6576313/how-to-avoid-undefined-offset
Delmo :

This worked for me:\n\n@list($func, $field) = explode('|', $value);\n",
2017-01-06T23:54:38
Jack Franklin :

You get an undefined offset when the thing you're trying to explode the string by ($value) doesn't actually have it in, I believe.\n\nThis question is very much similar to this: \nundefined offset when using php explode(), where there is a much further explanation which should fully solve your issue.\n\nAs for checking for the occurrence of '|' as to prevent the error, you can do:\n\n$pos = strpos($value,'|');\n\nif(!($pos === false)) {\n //$value does contain at least one |\n}\n\n\nHope this helps.",
2011-07-04T21:50:07
Andreas Jansson :

I'd probably break this up into two steps\n\n$split = explode('|', $value);\n$func = $split[0];\nif(count($split) > 1)\n $field = $split[1];\nelse\n $field = NULL;\n\n\nThere's probably a quicker and neater way though.",
2011-07-04T21:52:17
Dennis Crane :

if (count(explode('|', $value))==2)\n list($func, $field) = explode('|', $value);\n\n\nHowever it's slightly not optimal.",
2011-07-04T21:52:11
xtempore :

I often come across this issue, so I wanted a function that allowed something nicer syntactically without unnecessarily padding the array or string. \n\n// Put array entries in variables. Undefined index defaults to null\nfunction toVars($arr, &...$ret)\n{\n $n = count($arr);\n foreach ($ret as $i => &$r) {\n $r = $i < $n ? $arr[$i] : null;\n }\n}\n\n// Example usage\ntoVars(explode('|', $value), $func, $field);\n\n\nFor my purposes, I'm usually working with an array, but you could write a similar function that includes the explode function, like this...\n\n// Explode and put entries in variables. Undefined index defaults to null\nfunction explodeTo($delimiter, $s, &...$ret)\n{\n $arr = explode($delimier, $s);\n $n = count($arr);\n foreach ($ret as $i => &$r) {\n $r = $i < $n ? $arr[$i] : null;\n }\n}\n\n// Example usage\ntoVars('|', $value, $func, $field);\n\n\nRequires PHP5.6 or above for variadic function:\nhttp://php.net/manual/en/functions.arguments.php#functions.variable-arg-list",
2017-07-19T03:57:37
KingCrunch :

list($func, $field) = array_pad(explode('|', $value, 2), 2, null);\n\n\nTwo changes:\n\n\nIt limits the size of the array returned by explode() to 2. It seems, that no more than this is wanted\nIf there are fewer than two values returned, it appends null until the array contains 2 values. See Manual: array_pad() for further information\n\n\nThis means, if there is no | in $value, $field === null. Of course you can use every value you like to define as default for $field (instead of null). Its also possible to swap the behavior of $func and $field\n\nlist($func, $field) = array_pad(explode('|', $value, 2), -2, null);\n\n\nNow $func is null, when there is no | in $value.",
2011-07-04T21:56:04
Jon :

I don't know of a direct way to do this that also preserves the convenience of \n\nlist($func, $field) = explode('|', $value);\n\n\nHowever, since it's really a pity not to be able to do this, you may want to consider a sneaky indirect approach:\n\nlist($func, $field) = explode('|', $value.'|');\n\n\nI have appended to $value as many |s as needed to make sure that explode will produce at least 2 items in the array. For n variables, add n-1 delimiter characters.\n\nThis way you won't get any errors, you keep the convenient list assignment, and any values which did not exist in the input will be set to the empty string. For the majority of cases, the latter should not give you any problems so the above idea would work.",
2011-07-04T21:52:28
yy