[PHP] explode() Notice: Undefined offset 解決方法

1 min read
hiroweb developer

文字列を区切る関数。

概要

$str = "ab";
list($a, $b) = explode(',', $str);

こういう場合、以下の様なエラーが発生する。

NOTICE Undefined offset: 1 on line number 3

解決方法

array_pad()を使って埋める。

以下の様な感じで埋める。

list($a, $b) = array_pad(explode(',', $str), 2, null);