#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2009
    Posts
    94
    Rep Power
    6

    Associative array, find key, echo value. Stuck.


    Hi,

    I am trying to capture a value through an HTML form, but I cannot understand why the value from the array isn't being echo on the screen.

    All I want to do is search the array keys for a match, and then echo/print the matching value to the screen. Cannot find a built in function for it :-(

    Code:
    <?php
    	$myarray['a'] = 'string one';
    	$myarray['ab'] = 'string two';
    	$myarray['abc'] = 'string three';
    ?>
    
    <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
    	Name: <input type="text" name="name">
    	<input type="submit">
    </form>
    
    <?php
    	$findme = $_POST['name'];
    	echo array_search($findme,$myarray);
     ?>
    Can you why it isn't working?

    Thanks for any help : - )
  2. #2
  3. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    PHP Code:
    $myarray[$findme
    ??
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2009
    Posts
    94
    Rep Power
    6
    Don't understand what you are saying Jacques.


    Can anyone help me with this? Still stuck.
  6. #4
  7. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    You said you want the array value for a given key, right? Well, that's simply

    PHP Code:
    echo $myarray[$findme]; 
    If that's not what you want, you have to be more specific. What's the result you want to see on the screen?

    Comments on this post

    • Triffic agrees : Thank you
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2009
    Posts
    94
    Rep Power
    6
    Originally Posted by Jacques1
    You said you want the array value for a given key, right? Well, that's simply

    PHP Code:
    echo $myarray[$findme]; 
    If that's not what you want, you have to be more specific. What's the result you want to see on the screen?
    Thank you for your help Jacques, thats what I wanted and it's working now.

    Appreciate it
  10. #6
  11. Wiser? Not exactly.
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    May 2001
    Location
    Bonita Springs, FL
    Posts
    5,945
    Rep Power
    4033
    Note that you'll want to wrap that in an isset() check to prevent E_NOTICE errors in the event someone entered a key that does not exist in the array.

    Code:
    echo isset($myarray[$findme])?$myarray[$findme]:'Not found';
    Recycle your old CD's, don't just trash them



    If I helped you out, show some love with some reputation, or tip with Bitcoins to 1N645HfYf63UbcvxajLKiSKpYHAq2Zxud

IMN logo majestic logo threadwatch logo seochat tools logo