Jump to content

Verify a value in an Array


unemployment

Recommended Posts

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.

 

Link to comment
https://forums.phpfreaks.com/topic/228836-verify-a-value-in-an-array/
Share on other sites

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"

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.