wordpress check if value exist in array

I’m trying to compare one input field i get from submit form, But even if number is correct it won’t echo success.

global $wpdb;

$name = $_POST['name'];
$phone = $_POST['surname'];
$kunde = $_POST['kunde'];
$login = $wpdb->get_results( "SELECT kunde FROM wp_kundenumber", ARRAY_N );
$kunde_string = end(explode(' ', $kunde));



if(in_array($kunde_string, $login)){

echo "succes";
}
 else {
echo "failure";
}

EDIT: these are var dumps of variables:

Read More

for kunde_string it says string(6) "100001"

and $login is a big array array(9807) { [0]=> array(1) { [0]=> string(6) "100001" } [1]=> array(1) { [0]=> string(6) "100002" } [2]=> array(1) { [0]=> string(6) "100003" }...

Related posts

Leave a Reply

2 comments

  1. you can use array_map php function instead of foreach loop, then use in_array to find value..

    $login = $wpdb->get_results( "SELECT kunde FROM wp_kundenumber", ARRAY_N );
    $dataArray = array_map(function ($arr) {return $arr[0];}, $login); //will return single dimentional array of value at 0 index in $login items
    if(in_array($kunde_string, $dataArray)) ///then in_array will work for you.. to find $kunde_string
    {
        echo "Succes";
    }
    else 
    {
        echo "Failure";
    }
    
  2. Check in_array with foreach

    Try as follows

     global $wpdb;
    
        $name = $_POST['name'];
        $phone = $_POST['surname'];
        $kunde = $_POST['kunde'];
        $login = $wpdb->get_results( "SELECT kunde FROM wp_kundenumber", ARRAY_N );
        $kunde_string = end(explode(' ', $kunde));
        $flag = false; 
            foreach($login as $value)
            {
                if(in_array($kunde_string, $value, true))
                {
                   $flag = true; 
    
                } 
            }
            if($flag == true){
    
                  echo "succes";
            }else {
                 echo "failure";
            }