How to get specific value from array

Discussion in 'PHP' started by ketting00, Jun 27, 2011.

  1. #1
    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>&nbsp;</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
     
    ketting00, Jun 27, 2011 IP
  2. ntomsheck

    ntomsheck Peon

    Messages:
    87
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #2
    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
     
    ntomsheck, Jun 27, 2011 IP
  3. ketting00

    ketting00 Well-Known Member

    Messages:
    782
    Likes Received:
    28
    Best Answers:
    3
    Trophy Points:
    128
    #3
    Hmm!

    I do not get it. The value I want it displayed is the $level.
     
    ketting00, Jun 27, 2011 IP
  4. ntomsheck

    ntomsheck Peon

    Messages:
    87
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Misunderstood your question, sorry. Use the array_search function -

    $level = array_search("anymail@gmail.com",$email_array);
     
    ntomsheck, Jun 27, 2011 IP
  5. ketting00

    ketting00 Well-Known Member

    Messages:
    782
    Likes Received:
    28
    Best Answers:
    3
    Trophy Points:
    128
    #5
    It's still not working.
     
    ketting00, Jun 27, 2011 IP
  6. ntomsheck

    ntomsheck Peon

    Messages:
    87
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #6
    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)
     
    ntomsheck, Jun 27, 2011 IP
  7. ketting00

    ketting00 Well-Known Member

    Messages:
    782
    Likes Received:
    28
    Best Answers:
    3
    Trophy Points:
    128
    #7
    Thanks so much. I would try a switch/case then.
     
    ketting00, Jun 27, 2011 IP