0

I have the following if condition:

if ( 
    $items[ $count + 1 ]->menu_item_parent != $parent_id && 
    $submenu && 
    $items[ $count + 1 ]->menu_item_parent != $item->ID) {

    //something

}

I get the notice:

Undefined offset: 13

So I assume the 13 does not exist in the $items array and it can be avoided by checking with in_array. However I'm unsure how I can use that function in the above condition.

4
  • Is your condition in loop ? Commented Apr 30, 2015 at 8:15
  • You will want to have a look at the output of what you're testing on. What does that reveal? Commented Apr 30, 2015 at 8:15
  • Also instead of $count+1 can you use ++$count or $count++ whichever goes with you rather than that? Commented Apr 30, 2015 at 8:18
  • @PreetiMaurya In this case, $count+1 is necessary, because $count++ would actually alter the value of $count, whereas $count+1 does not (which lets him use that more than once). Commented Apr 30, 2015 at 8:29

4 Answers 4

7
if ( isset($items[ $count + 1 ]) && 
    $items[ $count + 1 ]->menu_item_parent != $parent_id && 
    $submenu && 
    $items[ $count + 1 ]->menu_item_parent != $item->ID) {

    //something

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

1 Comment

The idea is that, first need to check whether the items array's index is set or not before accesing that index element. So if that is set only it checks for the succeeding condtions otherwise it will return false and exit from the if condition.
3

You could first check if $items[$count + 1] exists. Like

if( 
   //check first if $items[$count + 1] exists, else it breaks here
   isset($items[ $count + 1 ] )

   //now you know $item[$count + 1] exists, so you can continue
   &&
     ( 
        $items[$count + 1]->menu_item_parent != $parent_id && 
        $submenu && 
        $items[ $count + 1 ]->menu_item_parent != $item->ID
     ) 
   ){

    //something

  }

Comments

1

You should use a prefix test using isset method.

The call would look like that:

if (isset($items[ $count + 1 ])) { //do stuffs}

Comments

1

Ok so the in_array docs say:

in_array ( mixed $needle , array $haystack)

Where

needle = the searched value

haystack = the array

And you are not looking for the value, you're looking for the key

When you look at the bottom under see also, it says:

which is what you are looking for:

array_key_exists() returns TRUE if the given key is set in the array. key can be any value possible for an array index.

Perfect! Exactly what you want!

array_key_exists ( mixed $key , array $array )

where

key = value to check

array = array (duh!)

if ( array_key_exists($count + 1, $items))
    $items[ $count + 1 ]->menu_item_parent != $parent_id && 
    $submenu && 
    $items[ $count + 1 ]->menu_item_parent != $item->ID) {

    //something

}

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.