OunknownO OunknownO - 5 months ago 26
PHP Question

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:

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" }...

Answer

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";
}