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
Try array_key_exists to see if it exists.
if( isset($option['color'][$key]) )
{
$_SESSION[$key]['option']['color'] = $option['color'][$key];
}
if it is always 0,1,2 or any line of consecutive integers you could do if(count($option['color']) > $key ){}
use isset
or empty
.
for example:
if (isset($array['idx'])){ ... }
if (!empty($array['idx'])){ ... }
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()
.