0

what i am looking for is a fast way to search up anything from these and get all fields back, for example if i search the sample code (given below) for "red" in favcolors, i get back name of the person whose kid's fave color this is, i.e. in this case, return an array containing jhon and homer. if the search term is for people with age = 43 then return homer, and so on...:

<?php
class person {
public $name;
public $kidsfavcolors=array();
public $age;
}

$people['jhon'] = new person;
$people['jhon']->name = "jhon";
$people['jhon']->age = 30;
$people['jhon']->kidsfavcolors['katherine']= "red";
$people['jhon']->kidsfavcolors['jimmy']= "yellow";

$people['homer'] = new person;
$people['homer']->name = "homer";
$people['homer']->age = 43;
$people['homer']->kidsfavcolors['bart']= "black";
$people['homer']->kidsfavcolors['lisa']= "red";
6
  • So basically you want to search recursively through all fields? Commented Jul 25, 2012 at 11:15
  • Not sure if there is anything built in for such a custom task. You would have to go about doing it the old way, that is implement a recursive search. Commented Jul 25, 2012 at 11:15
  • Use a DB and write queries :P Commented Jul 25, 2012 at 11:16
  • I suggest you write up some example function calls how you would like searching to work. Commented Jul 25, 2012 at 11:16
  • What is a recursive search? basically i want it to function like SELECT keyword in mySQL, but unfortunately this data can't be written in a DB Commented Jul 25, 2012 at 11:17

1 Answer 1

3

This could be a starting point:

function search_objects($objects, $key, $value) { // might contain bugs as I typed in in SO on the go
    $return = array();
    foreach ($objects as $object) {
        $objVars = get_object_vars($object);
        if (isset($objVars[$key]) && $objVars[$key] == $value) {
           $return[] = $object;
        }
    }
    return $return;
}

print_r(search_object($people, 'name', 'john'));

Without some kind of indexing it wont be much faster then linear walk of the objects. The complexity of this search is n*(avarage properties count). You will have to modify it to search in properties that are not simple key=>value but arrays. You might want to use reflections.

Where does the data come from? I'm rather optimistic there is a much better way to do this if we know more.

Sign up to request clarification or add additional context in comments.

4 Comments

what if index(key of array) is unknown?
Index is not unknown, it's just not existing in your case (you can create it). There are methods to index the objects by the fields they will be later searched on, it will cut time in expense of memory. But how many objects do you have? If it's a number with less then 5 zeroes, you might not need anything more complex then the solution I provided. Do not do premature optimizations costing you time, effort, complexity, when that is not neccessary. You sill haven't said why you want that, how will you use it and where the data comes from...
there are around 55,000 objects, you function dosent take much time, but one thing: it aint working! i replaced print_r in your function with var_dump and also fixed an typo on last line, but this is what i get as output:- array empty
sorry, your function works perfectly fine, its my wrong spelling of jhon :P

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.