954,574 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

assoc array search

Is there a better way to do a search within an array of associative arrays? :)

Basically a way to replace the function that I did that works, but was wondering if there's a better way.

<?php
$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");

$filtered = filter_array($employee, 'company', 'Acme inc.');
echo "<pre>";
echo "original array".print_r($employee,true)."";
echo "new array".print_r($filtered,true)."";
echo "</pre>";

//hack unless a better way is possible
function filter_array($hackstack, $fieldname, $needle)
{
$newarray = array();
foreach($hackstack as $key=>$subarray)
{
if($subarray[$fieldname]==$needle)
{
$newarray[] = $subarray;
}
}
return($newarray);
}

kireol
Posting Whiz
313 posts since Mar 2008
Reputation Points: 34
Solved Threads: 51
 

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.

darkagn
Veteran Poster
1,197 posts since Aug 2007
Reputation Points: 404
Solved Threads: 200
 

Thanks :)

kireol
Posting Whiz
313 posts since Mar 2008
Reputation Points: 34
Solved Threads: 51
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You