0

I have a chunk of PHP code that is giving me an Undefined index message.

$order = explode(',', $ass_icon_order);

foreach($order as $function){
    $function = (isset($function) ? $function : null);
    if($function != ''){
        $css .= '.ass-sortable-li.li-'.$function.' a:hover, .ass-sortable-li.li-'.$function.'.active a { background:'.$colors[$function].' }';
        ?>
        <li id="<?php echo $function; ?>" rel="<?php echo $function; ?>" class="ass-sortable-li li-<?php echo $function; ?>">
            <?php echo '<a href="#" class="icon-bg"><i class="ass-'.$function.'"></i></a>'; ?>
        </li>
    <?php
        $count_networks++;
    }
}

When I run a print_r($order) I get this array:

Array
(
    [0] => twitter
    [1] => facebook
    [2] => googleplus
    [3] => delicious
    [4] => stumbleupon
    [5] => pinterest
    [6] => linkedin
    [7] => youtube
    [8] => fblike
    [9] => fbshare
    [10] => fbtalk
    [11] => twitter_followers
    [12] => twitter_following
    [13] => twitter_tweets
    [14] => twitter_shares
    [15] => google_shares
    [16] => google_followers
    [17] => linkedin_shares
    [18] => linkedin_followers
    [19] => delicious_shares
    [20] => delicious_followers
    [21] => youtube_subscribers
    [22] => youtube_views
    [23] => dribbble
    [24] => soundcloud_followers
    [25] => soundcloud_plays
    [26] => instagram
    [27] => mailchimp
    [28] => foursquare
)

When I run print_r($colors) I get:

Array
(
    [fblike] => #3b5998
    [fbshare] => #3b5998
    [fbtalk] => #3b5998
    [twitter_followers] => #4ec2dc
    [twitter_following] => #4ec2dc
    [twitter_tweets] => #4ec2dc
    [twitter_shares] => #4ec2dc
    [google_shares] => #2d2d2d
    [google_followers] => #2d2d2d
    [linkedin_shares] => #006da7
    [linkedin_followers] => #006da7
    [delicious_shares] => #3271cb
    [delicious_followers] => #3271cb
    [stumbleupon] => #eb4924
    [youtube_subscribers] => #df1f1C
    [youtube_views] => #df1f1C
    [dribbble] => #f175a8
    [soundcloud_followers] => #ff4c00
    [soundcloud_plays] => #ff4c00
    [instagram] => #3b6a91
    [mailchimp] => #2C9AB7
    [foursquare] => #0732a2
    [pinterest] => #d01d15
)

I seem to be getting the notice from this section of code:

$css .= $colors[$function];

I have played around with adding isset, however I don't think I am doing the check correctly.

Any help would be greatly appreciated, thanks

11
  • 2
    $function = (isset($function) ? $function : null); -> $function = (isset($function) ? $function : ""); You assigned it with null not empty string Commented Feb 2, 2015 at 22:33
  • 3
    This code: $colors[$function] expects each element in $order to be a key in $colors, but NONE of the $order values you showed are present in $colors. You need to sanitize $order to make it match up with $colors. Commented Feb 2, 2015 at 22:34
  • Hi @JonathanKuhn, yes sorry it is defined further up in the code like this $colors = default_social_network_tab_colors(); Referring to a function I have created Commented Feb 2, 2015 at 22:35
  • 1
    @Jason If you made the changes from my first comment also change your if statement to this: if($function != '' && in_array($function, array_flip($colors))), does that do the trick for you? Commented Feb 2, 2015 at 22:46
  • 1
    Right, so you need $colors for twitter, facebook, etc. Commented Feb 2, 2015 at 22:49

1 Answer 1

1

You have to change this:

(Because NULL and empty string is not the same)

$function = (isset($function) ? $function : null);

to this:

$function = (isset($function) ? $function : "");
                                          //^^ See here

Also you have to change your if statement to this, so you check if the key exists in the array:

if($function != '' && in_array($function, array_flip($colors))) {
                    //^^^^^^^^ Check if '$function' exists in the array as key
Sign up to request clarification or add additional context in comments.

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.