garethhall Posted February 3, 2011 Share Posted February 3, 2011 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 More sharing options...
JonnoTheDev Posted February 3, 2011 Share Posted February 3, 2011 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); ?> Link to comment https://forums.phpfreaks.com/topic/226541-php-error-undefined-index/#findComment-1169356 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.