Compare array value

Discussion in 'PHP' started by fex, May 2, 2010.

  1. #1
    Hi,

    A small problem here but I can't get this to work... A pair of fresh eyes will help.

    I've got an array (with 15 elements) like this:
    
    $array_nl_data = array(1,0,1,0,0,0,0,0,0,0,0,0,0,0,0);
    
    Code (markup):
    I want - depending on the value in $array_nl_data - to call a different function:
    	
    function callForm($num){		
    	$i = $num - 1;
    		
    	if($array_nl_data[$i] == 0){
    		callText($num);
    		echo "result: ".$array_nl_data[$i];
    	}elseif($array_nl_data[$i] == 1){
    		callRadio($num);
    		echo "result: ".$array_nl_data[$i];
    	}
    }
    Code (markup):
    This code only executes callText(). Isn't it supposed to call callRadio() on $array_nl_data[0] and $array_nl_data[2] ?
     
    fex, May 2, 2010 IP
  2. ThePHPMaster

    ThePHPMaster Well-Known Member

    Messages:
    737
    Likes Received:
    52
    Best Answers:
    33
    Trophy Points:
    150
    #2
    Two things: If you are subtracting 1 from $num, you will never get a 1, always a 0 or -1. You also need to pass the function array_nl_data to the callForm function, as the array is not global and the function has no way of knowing what it is.
     
    ThePHPMaster, May 2, 2010 IP
  3. fex

    fex Peon

    Messages:
    89
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks for the fast help, works perfect ;)
     
    fex, May 2, 2010 IP