PHP错误 - 未定义的偏移量:1

时间:2011-04-16 04:53:26

标签: php drupal

当URL为domain.com/portfolio时,我收到php错误“undefined offset:1”,但对于domain.com/portfolio/project1则没有。非常感谢提前

$path = drupal_get_path_alias($_GET['q']);
$path = explode('/', $path);
if ($path[0] == 'portfolio' && $path[1] != '') {
    // action
}

3 个答案:

答案 0 :(得分:5)

if ($path[0] == 'portfolio' && !empty($path[1])) {

答案 1 :(得分:0)

if (($path[0] == 'portfolio') && (count($path) > 1) {
  // Do something
}

或者,如果您只想确保索引存在

$path = drupal_get_path_alias($_GET['q']);
$path = explode('/', $path);
$path = array_pad($path, 2, null);
if (($path[0] === 'portfolio') && (!is_null($path[1])) {
  // Do something
}

答案 2 :(得分:-1)

$path = drupal_get_path_alias($_GET['q']);
$path = explode('/', $path);
if ($path[0] == 'portfolio' && isset($path[1]) && $path[1] != '') {
    // action
}