How am I able to use an associative array key to search for a string and echo the value?

<?php
$haystack = "My name is Allen";

//works with this style array
$names = array("Mike", "Joe", "Bob", "allen");
echo arrayinstr($haystack, $names);//echo's allen

//How do I get associative style arrays to echo the sex (male) instead of "allen"? (male,female,alien)
$names["Bob"] = male;
$names["Sally"] = female;
$names["Charlie"] = male;
$names["Allen"] = male;
$names["Android"] = alien;
$names["ET"] = alien;
$nameS["Brad"] = male;


function arrayinstr($haystack, $needle) {
   foreach($needle as $value) {
   $haystack = strtolower($haystack);
      if (!strpos($haystack, $value) === false)
         $foundit = $value;
   }
   return $foundit;
}
?>
          if (!strpos($haystack, $value) === false) 

    But you're not wanting to look for the value, you're wanting to look for the key. Change the [man]foreach/man to get that as well.

      I'm very new to php but what curb described is what I am looking for. Not to steal the thread, can somebody post an example of what weedpacket was referring to for foreach() and looking for the key?

        Weedpacket linked to the PHP manual's entry on foreach. Take a look at that.

          Write a Reply...