Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    Regular Coder
    Join Date
    Jul 2002
    Location
    The Netherlands
    Posts
    252
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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

  • #2
    Senior Coder missing-score's Avatar
    Join Date
    Jan 2003
    Location
    UK
    Posts
    2,194
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #3
    Regular Coder
    Join Date
    Jun 2002
    Location
    Sheffield, UK
    Posts
    552
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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>); 
    Last edited by ReadMe.txt; 11-23-2003 at 02:06 PM.
    "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)

  • #4
    Senior Coder missing-score's Avatar
    Join Date
    Jan 2003
    Location
    UK
    Posts
    2,194
    Thanks
    0
    Thanked 0 Times in 0 Posts
    the only trouble with that would come is if you wanted !=, <, >, <= ect.

  • #5
    Regular Coder
    Join Date
    Jun 2002
    Location
    Sheffield, UK
    Posts
    552
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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)

  • #6
    Regular Coder
    Join Date
    Jul 2002
    Location
    The Netherlands
    Posts
    252
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •