how to use in_array

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • omerbutt
    Contributor
    • Nov 2006
    • 638

    how to use in_array

    hi i need to compare a value inside the array that is returned via recordset from database
    Code:
    Array
    (
        [0] => Array
            (
                [email] => ranatouqeer@msn.com
            )
    
        [1] => Array
            (
                [email] => omer@etgi.us
            )
    
    )
    i am trying to use in_array() to search inside the array but in_array() doesnt seem to do so here is the code
    [code=php]
    $email=$this->input->post('email' );
    if(in_array($em ail,$newsletter _array)){
    echo 'yes';
    }else{
    echo 'no';
    }
    [/code]
    i have tried using it this way too
    Code:
    if(in_array(array(array('email'=>$email)),$newsletter_array)){
    			echo 'yes';
    		}else{
    			echo 'no';
    		}

    what am i doing wrong
    regards,
    Omer Aslam
  • omerbutt
    Contributor
    • Nov 2006
    • 638

    #2
    i did it by using the following method
    Code:
    function array_value_recursive($key, array $arr){
    		$val = array();
    		@array_walk_recursive($arr, function($v, $k) use($key, &$val){ if($k == $key) array_push($val, $v);});
    		return count($val) > 1 ? $val : array_pop($val);
    	}
    $new_array=array_value_recursive('email',$newsletter_array);
    
    			if(in_array($email,$new_array)){
    echo 'matched';
    }else{
    echo 'not found';
    }
    regards,
    Omer Aslam

    Comment

    Working...