assoc array search

Thread Solved

Join Date: Mar 2008
Posts: 200
Reputation: kireol is an unknown quantity at this point 
Solved Threads: 27
kireol kireol is offline Offline
Posting Whiz in Training

assoc array search

 
0
  #1
Jun 18th, 2009
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<br>".print_r($employee,true)."<br>";
echo "new array<br>".print_r($filtered,true)."<br>";
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);
}
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 798
Reputation: darkagn has a spectacular aura about darkagn has a spectacular aura about darkagn has a spectacular aura about 
Solved Threads: 110
darkagn's Avatar
darkagn darkagn is offline Offline
Master Poster

Re: assoc array search

 
0
  #2
Jun 19th, 2009
There are two functions that you might find useful, in_array and array_search . Here is an example of their usage:
  1. $employee = array();
  2. $employee [] = array("name" =>"Chris Apple", "company" => "Acme inc.", "title" => "Developer", "salary" => "77000");
  3. $employee [] = array("name" =>"Todd Orange", "company" => "Daves Dogs", "title" => "Mgr", "salary" => "177000");
  4. $employee [] = array("name" =>"Gordon Banana", "company" => "Acme inc.", "title" => "Mgr", "salary" => "277000");
  5. $employee [] = array("name" =>"Kim Pear", "company" => "XYZ Signs", "title" => "sales", "salary" => "77000");
  6. $employee [] = array("name" =>"Steve Cherry", "company" => "self", "title" => "handyman", "salary" => "27000");
  7. $employee [] = array("name" =>"Jay Pineapple", "company" => "Acme inc.", "title" => "sales", "salary" => "47000");
  8.  
  9. // in_array usage
  10. foreach($employee as $record)
  11. {
  12. $flag = in_array("Acme.inc", $record);
  13. // here $flag is true if the string is in one of the inner arrays of the $employee array
  14. // note that because we are comparing to a string, the in_array function comparison is case sensitive
  15. // there is a third optional (boolean) parameter that can be passed into the in_array method
  16. // and if set to true this causes the comparison to be strict (ie === rather than ==) but default is false
  17. echo "Acme.inc in record = $flag";
  18. }
  19.  
  20. // array_search usage
  21. foreach($employee as $record)
  22. {
  23. $key = array_search("Acme.inc", $record);
  24. // here $key is equal to the first key (index) of the record that contains the string "Acme.inc"
  25. // in this example, for the first record, the $key will equal "company" as it will for the third and sixth records
  26. // it will be identical (===) to false for the other records, so if we want to do something with this $key we need to check
  27. // again the string is case sensitive, and a third parameter allows you to set strict comparisons
  28. if($key === false)
  29. echo "Record not found.";
  30. else
  31. echo "Record with key = $key has acme string.";
  32. }

I believe with a combination of these two functions, you could successfully redo your custom function in just a few lines of code.
There are no stupid questions, only those too stupid to ask for help.
echo is a web developer's best friend.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 200
Reputation: kireol is an unknown quantity at this point 
Solved Threads: 27
kireol kireol is offline Offline
Posting Whiz in Training

Re: assoc array search

 
0
  #3
Jun 19th, 2009
Thanks
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC