Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-22-2003, 08:15 PM   PM User | #1
Michiel
Regular Coder

 
Join Date: Jul 2002
Location: The Netherlands
Posts: 252
Thanks: 0
Thanked 0 Times in 0 Posts
Michiel is an unknown quantity at this point
Select data form array that matches condition

Hi,

I've got a question about selecting data from an array. I've got an array with the following format:

$db[0]['name'] = 'john'
$db[0]['age'] = 18
$db[0]['gender'] = 'm'

$db[1]['name'] = 'janet'
$db[1]['age'] = 17
$db[1]['gender'] = 'f'

etc.

I want to select the array keys of the items that match a condition that is constructed as 'gender = m' or 'age > 10'. I want to write a function that can return an array that contains all the keys of the array $db that match the condition.

E.g. if the condition 'gender = m' is used the function should return: $return = array(0)

And if the condition is 'age > 10' the function should return $return = array(0,1)

How would I go about that? What functions should I examine further? Can anyone push me in the right direction?

I hope I've been clear enough ... if not please let me know and I'll elaborate. Thanx in advance for your replies!

Cheers, Michiel
Michiel is offline   Reply With Quote
Old 11-22-2003, 08:54 PM   PM User | #2
missing-score
Senior Coder


 
missing-score's Avatar
 
Join Date: Jan 2003
Location: UK
Posts: 2,194
Thanks: 0
Thanked 0 Times in 0 Posts
missing-score is on a distinguished road
try this, note that for = you should use ==, it makes it easier.

PHP Code:

<?php
$matches 
= array();
$condition 'gender == m';

for(
$i=0;$i<count($db);$i++){

   
$condition_parts explode(" "$condition);
   
$condition_parts[2] = (preg_match("/^([0-9]*)$/is"$condition_parts[2]) ? $condition_parts[2] : "\\\\\\"".$condition_parts[2].""");   

   eval(
"if(\\$db[\$i]['".$condition_parts[0]."'] ".$condition_parts[1]." ".$condition_parts[2]."){
      array_push(\\$matches, \\$i);
   }"
);

}



?>
and $matches would be your returned results. This is untested, i will have a test and come back in a minute.
missing-score is offline   Reply With Quote
Old 11-23-2003, 01:59 PM   PM User | #3
ReadMe.txt
Regular Coder

 
Join Date: Jun 2002
Location: Sheffield, UK
Posts: 552
Thanks: 0
Thanked 0 Times in 0 Posts
ReadMe.txt is an unknown quantity at this point
slightly cut down version not using regexp:
PHP Code:
function findKeys($array,$field,$value) {
foreach (
$array as $key=>$info) {
if (
$info[$field] == $value) {
$matches[] = $key;
}
}
return 
$matches;

now you just pass it as a function like so:
PHP Code:
$matches findKeys($db,'name','John'); 
To find keys of all elements where name == 'John' in the arrray $db.

Also untested but i cant see any obvious mistakes off hand, i'll test it if i get a chance in a minute.


EDIT:
PHP Code:
<?php

function findKeys($array,$field,$condition,$value) {
foreach (
$array as $key=>$info) {
eval(
'if ($info[$field] '.$condition.' $value) {
$matches[] = $key;
}'
);
}
return 
$matches;
}

$db[0]['name'] = 'john';
$db[0]['age'] = 18;
$db[0]['gender'] = 'm';

$db[1]['name'] = 'janet';
$db[1]['age'] = 17;
$db[1]['gender'] = 'f';

print_r(findKeys($db,'age','>','10'));

?>
I re-read you post and noticed the bit about the condition, so i altered my function slightly, this example inputs your sample data and outputs the results, syntax is now
PHP Code:
findKeys(<the array>,<field to search>,<condtion>,<value>); 
__________________
"To be successful in IT you don't need to know everything - just where to find it in under 30 seconds"

(Me Me Me Me Me Me Me Me Me)

Last edited by ReadMe.txt; 11-23-2003 at 02:06 PM..
ReadMe.txt is offline   Reply With Quote
Old 11-23-2003, 02:01 PM   PM User | #4
missing-score
Senior Coder


 
missing-score's Avatar
 
Join Date: Jan 2003
Location: UK
Posts: 2,194
Thanks: 0
Thanked 0 Times in 0 Posts
missing-score is on a distinguished road
the only trouble with that would come is if you wanted !=, <, >, <= ect.
missing-score is offline   Reply With Quote
Old 11-23-2003, 02:08 PM   PM User | #5
ReadMe.txt
Regular Coder

 
Join Date: Jun 2002
Location: Sheffield, UK
Posts: 552
Thanks: 0
Thanked 0 Times in 0 Posts
ReadMe.txt is an unknown quantity at this point
heh, just fixed that.
__________________
"To be successful in IT you don't need to know everything - just where to find it in under 30 seconds"

(Me Me Me Me Me Me Me Me Me)
ReadMe.txt is offline   Reply With Quote
Old 11-23-2003, 08:53 PM   PM User | #6
Michiel
Regular Coder

 
Join Date: Jul 2002
Location: The Netherlands
Posts: 252
Thanks: 0
Thanked 0 Times in 0 Posts
Michiel is an unknown quantity at this point
Thanx guys!

That has realy helped me a lot! Looks quite simple as well, damn why didn't I figure that out myself

Cheers! Michiel
Michiel is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 05:52 AM.


Advertisement
Log in to turn off these ads.