943,923 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Marked Solved
  • Views: 1120
  • PHP RSS
Jun 18th, 2009
0

assoc array search

Expand Post »
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);
}
Similar Threads
Reputation Points: 34
Solved Threads: 51
Posting Whiz
kireol is offline Offline
305 posts
since Mar 2008
Jun 19th, 2009
0

Re: assoc array search

There are two functions that you might find useful, in_array and array_search . Here is an example of their usage:
php Syntax (Toggle Plain Text)
  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.
Reputation Points: 395
Solved Threads: 192
Veteran Poster
darkagn is offline Offline
1,136 posts
since Aug 2007
Jun 19th, 2009
0

Re: assoc array search

Thanks
Reputation Points: 34
Solved Threads: 51
Posting Whiz
kireol is offline Offline
305 posts
since Mar 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: Help moving thumbnails
Next Thread in PHP Forum Timeline: cannot login to a new page using cURL..shows 404 error..!!PLZ HELP..!!





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC