There are two functions that you might find useful,
in_array and
array_search . Here is an example of their usage:
$employee = array();
$employee [] = array("name" =>"Chris Apple", "company" => "Acme inc.", "title" => "Developer", "salary" => "77000");
$employee [] = array("name" =>"Todd Orange", "company" => "Daves Dogs", "title" => "Mgr", "salary" => "177000");
$employee [] = array("name" =>"Gordon Banana", "company" => "Acme inc.", "title" => "Mgr", "salary" => "277000");
$employee [] = array("name" =>"Kim Pear", "company" => "XYZ Signs", "title" => "sales", "salary" => "77000");
$employee [] = array("name" =>"Steve Cherry", "company" => "self", "title" => "handyman", "salary" => "27000");
$employee [] = array("name" =>"Jay Pineapple", "company" => "Acme inc.", "title" => "sales", "salary" => "47000");
// in_array usage
foreach($employee as $record)
{
$flag = in_array("Acme.inc", $record);
// here $flag is true if the string is in one of the inner arrays of the $employee array
// note that because we are comparing to a string, the in_array function comparison is case sensitive
// there is a third optional (boolean) parameter that can be passed into the in_array method
// and if set to true this causes the comparison to be strict (ie === rather than ==) but default is false
echo "Acme.inc in record = $flag";
}
// array_search usage
foreach($employee as $record)
{
$key = array_search("Acme.inc", $record);
// here $key is equal to the first key (index) of the record that contains the string "Acme.inc"
// in this example, for the first record, the $key will equal "company" as it will for the third and sixth records
// it will be identical (===) to false for the other records, so if we want to do something with this $key we need to check
// again the string is case sensitive, and a third parameter allows you to set strict comparisons
if($key === false)
echo "Record not found.";
else
echo "Record with key = $key has acme string.";
}
I believe with a combination of these two functions, you could successfully redo your custom function in just a few lines of code.
Reputation Points: 395
Solved Threads: 192
Veteran Poster
Offline 1,136 posts
since Aug 2007