i am trying to switch array value but i get this error message: Undefined offset 2

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Mary meer

    #1

    i am trying to switch array value but i get this error message: Undefined offset 2

    i am trying to switch array value but i get this error message: Undefined offset 2

    Code:
    $at=array(
    						  array('Title:','title',2,10),
    						  array('First Name:','fname',2,10 ),
    i have foreach loop
    Code:
    foreach($at[$s] as $v){
    		
    switch ($v[2]){
    
    case 2:
    if(strlen($_POST[$v[1]]> strlen($_POST($v[2]))   ){
    echo 'ok';	
    	 }else{
    		$ok=false;
    	}
    break;
    Last edited by Atli; Nov 7 '10, 12:50 PM. Reason: Please use [code] tags when posting code.
  • Atli
    Recognized Expert Expert
    • Nov 2006
    • 5062

    #2
    Line #6 does this $_POST($v[2]).

    Two problems:
    1. You should use brackets to fetch array elements, not parenthesis. That is, do: $_POST[$v[2]] instead.
    2. The value of $v[2] in both the examples you posted is 2. - If your $_POST data does not contain a value for a key named "2", you will get the error message you posted.

      You should always use the isset function on input variables before trying to use them. There is always a possibility that they were not sent, and you need to write you code so it can handle that. (That is; output an error message or something.)

    Comment

    Working...