HELP! Selecting Array Rows based on an array of field=>value s



Here's the code i have:

$db = array(); //main array
$db['main'] = array(); //table `main`
$db['main'][] = array('id'=>0,
'username'=>'joshfrench','userpass'=>'password','userlevel'=>'admin');
//row
$db['main'][] = array('id'=>1,
'username'=>'bob_smith','userpass'=>'psswrd','userlevel'=>'user');
//row

function trim_value(&$value){
$value = strtolower(trim($value));
}
function fdb_select($fields = "*", $table, $where = false, $limit =
false){
global $db;
$retv = $db[$table];

//LIMIT RESULTS TO FIELDS
if(strrpos($fields, ",") === false){$field=array($fields);}else{$field
= explode(",", $fields);};
array_walk($field, "trim_value");
$all_fields = array_keys($retv[0]);
if($fields == '*'|| is_null($fields)){$field = $all_fields;};
$field = array_uintersect($all_fields, $field, "strcasecmp");
$retvi = array();
foreach($retv as $num => $row){
foreach($row as $fname => $fvalue){
$fname1 = strtolower($fname);
if(in_array($fname1, $field)){
$retvi[$num][$fname]=$fvalue;
};
};
};
//DONE LIMITING TO FIELDS
if($where != false){

//UP TO HERE IT WORKS FINE
//LIMIT TO WHERE CLAUSE
$retvj = array();
foreach($retvi as $num => $row){
//IN THE ROW
foreach($where as $k => $v){
//IF ROW MEETS ALL REQUIREMENTS
if($row[$k] == $v){
$retvj[] = $retvi[$num]; //Keep that row
};
};
};
//DONE LIMITING TO WHERE CLAUSE
};
$retvi = (isset($retvj)) ? $retvj:$retvi;
//SEND A FINAL ASSOCIATIVE ARRAY
return $retvi;
};
print_r(fdb_select("*", "main", array('id'=>"1",
"username"=>"joshfrench")));

the previous code outputs this:

Array
(
[0] => Array
(
[id] => 0
[username] => joshfrench
[userpass] => password
[userlevel] => admin
)

[1] => Array
(
[id] => 1
[username] => bob_smith
[userpass] => psswrd
[userlevel] => user
)

)

if i make the last option array('id'=>'1'), it does what i want.

HELP!!

.



Relevant Pages

  • Re: 2 extreme newbie ?s
    ... > completely oblivious as to which libraries to look in. ... > Dim num As Double ... > If I want num to be set to 8.93, which method of the String class should I ... > clause to function the way I want it to. ...
    (microsoft.public.dotnet.academic)
  • VB Question
    ... I am wondering if it is possible to change the WHERE clause of this statement ... Lets say on a search form a user searchs for a record, ... INSERT INTO SRMisc ([Tracker Item], Description, Comments, Requestor, [SR ... Num]) ...
    (microsoft.public.access.forms)
  • Re: DBI/SQL question
    ... statement using a "like" clause. ... foreach my $num { ... like # I draw a blank here ... I am not sure how to use the $num in a like since a like is '%' and it I do '%$num' that won't be ierpolated. ...
    (perl.beginners)
  • DBI/SQL question
    ... I have a large array of numbers that I need to use in an update SQL statement using a "like" clause. ... foreach my $num { ... equip like # I draw a blank here ... I am not sure how to use the $num in a like since a like is '%' and it I do '%$num' that won't be interpolated. ...
    (perl.beginners)
  • DBI/SQL question
    ... statement using a "like" clause. ... foreach my $num { ... equip like # I draw a blank here ... I am not sure how to use the $num in a like since a like is '%' and it ...
    (comp.lang.perl.misc)