1

There is post of select form like:

<select name="option[color][0]">
<select name="option[color][1]">
// option[color][2] isnt posted

Some products doesnt have that select, and then when I try to get them from post, each time if select isn't posted, im getting error like:

Undefined offset: 2

How to check if something is posted? Tried:

$ids       = $_POST['id'];
$option = $_POST['option'];

foreach ($ids as $key => $id)
{
   //Undefined offset: 2
   if( $option['color'][$key] )
   {
      $_SESSION[$key]['option']['color'] = $option['color'][$key];
   }

   //Undefined offset: 2
   if( !empty($option['color'][$key]) )
   {
      $_SESSION[$key]['option']['color'] = $option['color'][$key];
   }

   //Undefined offset: 2
   if( isset($option['color'][$key]) )
   {
      $_SESSION[$key]['option']['color'] = $option['color'][$key];
   }

   //... etc
}

Etc.... what ever I try, there is error :( Please help

5 Answers 5

2

Try array_key_exists to see if it exists.

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

Comments

1

isset($option['color'][$key]) is the way to go.

Check the exact line of code the error occurs when you still get it using isset().

Comments

1

if it is always 0,1,2 or any line of consecutive integers you could do if(count($option['color']) > $key ){}

2 Comments

sorry should be just > not >=
This also workd, but allready gave best answer. Thanks one more time
0
if( isset($option['color'][$key]) )
{
  $_SESSION[$key]['option']['color'] = $option['color'][$key];
}

Comments

0

use isset or empty.

for example:

if (isset($array['idx'])){ ... }
if (!empty($array['idx'])){ ... }

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.