0

I have this code:

print_r(array_keys($variables));
if (array_key_exists('form', $variables)) {
 print "YES!";
}
$imgs = $variables['form']['field_images'];

It's a part of the code that I use to theme a form page in Drupal. YES is printed out, however, drupal reports undefined index for that. Thanks for your generous help

1
  • 2
    Are you sure it's not reporting that field_images is undefined? Commented Jul 30, 2012 at 7:41

3 Answers 3

1

$variables['form'] does exist, but $variables['form']['field_images] probably not. That's why you get the notice about undefined index.

So you should make sure that the subkey also exists before you are calling it.

Sign up to request clarification or add additional context in comments.

Comments

0

as an example implemenation of Ikke`s answer:

if ( !array_key_exists('form', $variables) ) {
    echo 'missing parameter form';
}
else if ( !array_key_exists('field_images', $variables['form']) ) {
        echo 'missing parameter field_images';
}
else {
    $imgs = $variables['form']['field_images'];     
}

Comments

0

Try this:-

PHP throws the notice. You can add an isset() or !empty() check to avoid the error, like such:

if(isset($variables)) ) && !empty($variables)) ))
{

    if (array_key_exists('form', $variables)) {
     print "YES!";
    }
    $imgs = $variables['form']['field_images'];

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.