How to avoid undefined offset

Php

Php Problem Overview


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?

Php Solutions


Solution 1 - Php

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

Two changes:

  • It limits the size of the array returned by explode() to 2. It seems, that no more than this is wanted
  • If there are fewer than two values returned, it appends null until the array contains 2 values. See Manual: array_pad() for further information

This 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

list($func, $field) = array_pad(explode('|', $value, 2), -2, null);

Now $func is null, when there is no | in $value.

Solution 2 - Php

I don't know of a direct way to do this that also preserves the convenience of

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

However, since it's really a pity not to be able to do this, you may want to consider a sneaky indirect approach:

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

I 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.

This 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.

Solution 3 - Php

This worked for me:

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

Solution 4 - Php

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.

This question is very much similar to this: https://stackoverflow.com/questions/1807849/undefined-offset-when-using-php-explode, where there is a much further explanation which should fully solve your issue.

As for checking for the occurrence of '|' as to prevent the error, you can do:

$pos = strpos($value,'|');

if(!($pos === false)) {
     //$value does contain at least one |
}

Hope this helps.

Solution 5 - Php

I'd probably break this up into two steps

$split = explode('|', $value);
$func = $split[0];
if(count($split) > 1)
  $field = $split[1];
else
  $field = NULL;

There's probably a quicker and neater way though.

Solution 6 - Php

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

However it's slightly not optimal.

Solution 7 - Php

I often come across this issue, so I wanted a function that allowed something nicer syntactically without unnecessarily padding the array or string.

// Put array entries in variables. Undefined index defaults to null
function toVars($arr, &...$ret)
{
    $n = count($arr);
    foreach ($ret as $i => &$r) {
        $r = $i < $n ? $arr[$i] : null;
    }
}

// Example usage
toVars(explode('|', $value), $func, $field);

For my purposes, I'm usually working with an array, but you could write a similar function that includes the explode function, like this...

// Explode and put entries in variables. Undefined index defaults to null
function explodeTo($delimiter, $s, &...$ret)
{
    $arr = explode($delimier, $s);
    $n = count($arr);
    foreach ($ret as $i => &$r) {
        $r = $i < $n ? $arr[$i] : null;
    }
}

// Example usage
toVars('|', $value, $func, $field);

Requires PHP5.6 or above for variadic function: http://php.net/manual/en/functions.arguments.php#functions.variable-arg-list

Solution 8 - Php

Want to mention a more general utility function that I use since decades. It filters out empty values and trims spaces. It also uses array_pad() to make sure you get at least the requested amount of values (as suggested by @KingCrunch).

/**
 * Does string splitting with cleanup.
 * Added array_pad() to prevent list() complaining about undefined index
 * @param $sep string
 * @param $str string
 * @param null $max
 * @return array
 */
function trimExplode($sep, $str, $max = null)
{
	if ($max) {
		$parts = explode($sep, $str, $max); // checked by isset so NULL makes it 0
	} else {
		$parts = explode($sep, $str);
	}
	$parts = array_map('trim', $parts);
	$parts = array_filter($parts);
	$parts = array_values($parts);
	$parts = array_pad($parts, $max, null);
	return $parts;
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionclarkkView Question on Stackoverflow
Solution 1 - PhpKingCrunchView Answer on Stackoverflow
Solution 2 - PhpJonView Answer on Stackoverflow
Solution 3 - PhpDelmoView Answer on Stackoverflow
Solution 4 - PhpJack FranklinView Answer on Stackoverflow
Solution 5 - PhpAndreas JanssonView Answer on Stackoverflow
Solution 6 - PhpDennis CraneView Answer on Stackoverflow
Solution 7 - PhpxtemporeView Answer on Stackoverflow
Solution 8 - PhpSlawaView Answer on Stackoverflow