0

Here's what I have

foreach ( $post_formats as $format ) {
    if ( $options['show_post_formats'][$format] == 0 ) {
        $format = 'post-format-' . $format;
        array_push( $hide, $format );
    }
}

and it works fine ... but gives me an Undefined index: error when I debug because it wants the value of $format to be in quotes. How would I do this properly?

5
  • We need to see more code Commented Feb 12, 2013 at 0:05
  • ok but it's the $format is what the debugger has a problem with because it's expecting the value to be in quotes Commented Feb 12, 2013 at 0:07
  • As @MathieuImbert stated, more source would be of great use, but you can check if an index exists with isset Commented Feb 12, 2013 at 0:08
  • @byronyasgur A specific $format doesn't exist in your array, you should go with Lawrence's answer. Commented Feb 12, 2013 at 0:09
  • think I might have had the wrong idea about what the problem was Commented Feb 12, 2013 at 0:17

1 Answer 1

3

as your not sure the index will be there you simply use !empty() and check the array key exists.

<?php 
foreach ( $post_formats as $format ) {
    if (!empty($format) && array_key_exists($format, $options['show_post_formats']) && $options['show_post_formats'][$format] == 0 ) {
        $format = 'post-format-' . $format;
        array_push( $hide, $format );
    }
}
?>
Sign up to request clarification or add additional context in comments.

2 Comments

not sure I understand ... do I drop the entire foreach into this?
There was no foreach when I answered, anyways $format could possible be an empty value or blank thus causing the problem. Updated

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.