| | |
assoc array search
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Thread Solved |
•
•
Join Date: Mar 2008
Posts: 200
Reputation:
Solved Threads: 27
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.

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);
} There are two functions that you might find useful,
I believe with a combination of these two functions, you could successfully redo your custom function in just a few lines of code.
in_array and array_search . Here is an example of their usage: php Syntax (Toggle Plain Text)
$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.
There are no stupid questions, only those too stupid to ask for help.
echo is a web developer's best friend. ![]() |
Similar Threads
- array search (PHP)
- Fastest way to search the table ( or array ) (C++)
- Words Into Array (C++)
- .txt/array problems (C++)
- Saving from an altered Array (VB.NET)
- search and results question. (PHP)
- multi dimensional array search xml parser (PHP)
- Pagination - not displaying results properly, please help! (PHP)
- search and replace (Java)
Other Threads in the PHP Forum
- Previous Thread: Help moving thumbnails
- Next Thread: php biometrics (fingerprint)-Help pls
| Thread Tools | Search this Thread |
.htaccess ajax apache api array back basic beginner binary broken cakephp checkbox class cms code computing cron curl customizableitems database date delete display dynamic echo email error file files filter folder form forms function functions gc_maxlifetime google host href htaccess html image include insert integration ip java javascript joomla limit link login loop mail memmory memory menu mlm mod_rewrite multiple mysql navigation oop parsing paypal pdf php problem query radio random recursion regex remote script search server sessions sms snippet soap source space sql syntax system table thesishelp trouble tutorial update upload url validation validator variable video web xml youtube





