Jump to content

Php error: Undefined index:


garethhall

Recommended Posts

Hi I am working on a php module and I am getting this "Notice: Undefined index:" Error.

 

Now I know that I can change my php settings to not show the error but I would like to fix it properly.

 

What am I doing wrong?

 

<?php
function _com_slider_image_styles() {
  $styles = &drupal_static(__FUNCTION__);

  // Grab from cache or build the array.
  if (!isset($styles)) {
    if ($cache = cache_get('image_styles', 'cache')) {
      $styles = $cache->data;
    }
    else {
      // Select all the user-defined styles.
      $user_styles = db_select('image_styles', NULL, array('fetch' => PDO::FETCH_ASSOC))
        ->fields('image_styles')
        ->orderBy('name')
        ->execute()
        ->fetchAllAssoc('name', PDO::FETCH_ASSOC);
    }
  }

  $com_styles = null;
  foreach($styles as $key => $value){
	$com_styles[$key] .= $key; // PROBLEM HERE!
}
  return $com_styles;
}
?>

Link to comment
https://forums.phpfreaks.com/topic/226541-php-error-undefined-index/
Share on other sites

I do not understand why you are constructing a 2nd array of keys using the key as an index when the data is available in the $styles array. Simply return the array keys if you needs them in a new array.

 

<?php
$keys = array_keys($styles);
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.