Jump to content

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.