Hi, I've tested trying to get a specific value from a form using an array but it's not working? How do I alter the code to get it worked? <?php $email_array[0] = "anymail@gmail.com"; $email_array[1] = "othermail@gmail.com"; $email_array[2] = ""; $level = $email_array['']; if($email_array[''] == 0) { $level = 9; } elseif($email_array[''] == 1) { $level = 8; } elseif($email_array[''] == 2) { $level = 7; } ?> <html> <body> <center> <div align="left" style="width: 330px;"> <form name="email" method="post" action="<?php echo ($_SERVER['php_self']); ?>"> <fieldset><legend>Authentication</legend> <label>Email</label><input id="email" type="text" name="email"><br /> <label> </label><input id="submit" type="submit" name="submit" value="Login"><br /> </fieldset> </form> </div> <br /> <br /> <br /> <?php $email = trim($_POST['email']); if(empty($_POST['email'])) { echo "The field must not empty."; } elseif(!(preg_match("/^[\.A-z0-9_\-\+]+[@][A-z0-9_\-]+([.][A-z0-9_\-]+)+[A-z]{1,4}$/", $_POST['email']))) { echo "You haven't provide a valid email."; } else { echo $level; } ?> </center> </body> </html> Code (markup): Thanks in advance
You're very close - you have to put a number (or an index) in the array identifier, not a missing character. $email_array[0] will return
Misunderstood your question, sorry. Use the array_search function - $level = array_search("anymail@gmail.com",$email_array);
Well the exact code you typed isn't going to work, you have to update it to support the new value. I was merely showing you how to return the index from a given e-mail address, then use that index to calculate the level in your if/else statement (a switch/case statement would be better, but I'll keep it simple). I'll give you a hint: instead of if(email_array[''] == 0) use if($level == 0) { $level = 9 (No reason to create new variables if the old one isn't going to be used again)