unemployment Posted February 25, 2011 Share Posted February 25, 2011 I need to verify a submitted value with one I have in an array. Example... <input type="hidden" name="page" id="fb_page" value="<?php echo htmlentities(substr($_SERVER['SCRIPT_NAME'], 1)); ?>" /> $pages = array( '1' => 'One', '2' => 'Two', '3' => 'Three', '4' => 'Four' ); I need to say if the value == 'One' then echo 1. Keep mind that the array is in an external file. The post is in the footer and the echoed data is in another file. Essentially I am posting '1' to the database and then I want 'One' echoed out if it matches the value in the array. Quote Link to comment Share on other sites More sharing options...
Psycho Posted February 25, 2011 Share Posted February 25, 2011 array_search(): http://www.php.net/manual/en/function.array-search.php Searches the array for a given value and returns the corresponding key if successful You will need to take into account the "case" of the input. It will need to be the same case as the value in the array. I.e. "One" != "one" Quote Link to comment Share on other sites More sharing options...
samshel Posted February 25, 2011 Share Posted February 25, 2011 if(isset($pages[$submittedvalue])) echo $pages[$submittedvalue]; Quote Link to comment Share on other sites More sharing options...
Psycho Posted February 25, 2011 Share Posted February 25, 2011 if(isset($pages[$submittedvalue])) echo $pages[$submittedvalue]; based upon the OP comments he needs to search the array VALUES against the user input and return the index. The above would work if you were searching the array indexes and wanted the value. array_search() is the correct solution. Quote Link to comment Share on other sites More sharing options...
samshel Posted February 25, 2011 Share Posted February 25, 2011 I agree. Sorry thought it was other way round. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.