0

I have an array named results_array:

var_dump($results_array):
0 => php,mysql,jquery,ruby,html,css,lamp
1 => mouse,keyboard,laptop,pad
2 => table,sofa,caption
3 => control,television,control television,television control

and I have a $q which stands for query, I want to search in the $results_array and remove the items which has nothing to do with the query, so if I set $q=a then results array should be this:

0 => lamp
1 => keyboard,laptop,pad
3 => table,sofa,caption
4 => empty

now, I want to put the above results in each index of the results_array, at the end results_array should be:

0 => lamp
1 => keyboard
2 => laptop
3 => pad
4 => table
5 => sofa
6 => caption

my code is:

foreach($results_array as &$row) {
            $row = explode(',', $row);
        }
        unset($row);
        $results_array = call_user_func_array('array_merge_recursive', $results_array);

        foreach ($array as $k => $v) {
            if(strpos($v, 'a') === false) unset($array[$k]);
        }
        $results_array = array_values($results_array);

this exactly does what I need, now I set my $q = 'tele co', now after applying the above code set $ = 'tele co', it returns emtpy, but it should not because in:

3 => control,television,control television,television control

'control television,television control' should returned, but it's not, so I change my code to:

foreach($results_array as &$row) {
            $row = explode(',', $row);
        }
        unset($row);
        $results_array = call_user_func_array('array_merge_recursive', $results_array);
// HERE IS CHANGED ****
        $q = preg_split('/\s+/', trim($q));
        foreach ($results_array as $key => $value) {
            foreach ($q as $query) {
                if (stripos($value, $query) === false) {
                    unset($results_array[$key]);
                    break;
                }
            }
        }
// ****
        $results_array = array_values($results_array);

it's still not working, by passing $q = 'tele co' I need that 'television control' and 'control television' return, because they both got 'tele' AND 'co' which is in $q

I appreciate any kind of help

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
behz4d
  • 1,819
  • 5
  • 35
  • 59
  • Are you pulling this stuff from a database? If yes, what is your table structure and/or queries you're using? – NullUserException Nov 14 '12 at 17:51
  • yes, they are from db, they are fields of interest of eahc user forexample which are separated by ',', we've got a table members and a col fields, the data are coming from them, I want to auto suggest them matched result – behz4d Nov 14 '12 at 17:54
  • Therein lies the problem. You should **not** save serialized values (eg: comma-separated) in a relational database. It will lead to problems like this one, and more. (see [this answer](http://stackoverflow.com/questions/7364803/storing-arrays-in-the-database/7364834#7364834)). Implement [many-to-many the right way](http://stackoverflow.com/questions/7296846/how-to-implement-one-to-one-one-to-many-and-many-to-many-relationships-while-des/7296873#7296873). – NullUserException Nov 14 '12 at 17:56
  • Your code seems to be working...http://codepad.org/kJLsmdaQ – air4x Nov 14 '12 at 18:04
  • @NullUserException does this put pressure on the server? why i should NOT, as you see I did the search, this just have a little problem which is gonna be fixed, are you concerned about performance? – behz4d Nov 14 '12 at 18:04
  • @behz4d What kind of "pressure" are you talking about? It's not just a performance issue (it will be faster if you do it the right way and have the database return only what you want rather than return everything and process the results in PHP), it's about doing things the right way. – NullUserException Nov 14 '12 at 18:10
  • I can almost guarantee this won't be the only problem you'll run into, forcing you to use a workaround. For example, how do you update the first set of results, eg: remove ruby, or add python (which would also force you to check whether or not python is already there)? How do you search for just php? How would you search for php **and** ruby? How about php **or** ruby? All these are much easier and faster to do with a proper many-to-many structure. – NullUserException Nov 14 '12 at 18:12
  • @NullUserException you mean to have another table as 'members_fields', insert each field as a row? – behz4d Nov 14 '12 at 18:16
  • @behz4d Depends on whether it's a one-to-many or many-to-many relationship. If members can share the same fields (eg: member1 and member2 can both have field3, and field4 can belong to member1 and member4), it's a many-to-many relationship. In this case, you'd need a `fields` table and a `members_fields` junction table linking the two. – NullUserException Nov 14 '12 at 18:19

2 Answers2

1

Assuming:

$q = 'a c';
$results_array = array(
    'php,mysql,jquery,ruby,html,css,lamp',
    'mouse,keyboard,laptop,pad',
    'table,sofa,caption',
    'control,television,control television,television control'
);

Solution:

$result = array_map(function ($element) use ($q) {
    $words = explode(',', $element);
    return array_filter($words, function ($word) use ($q) {
        $qwords = explode(' ', $q);
        return count(array_filter($qwords, function ($qword) use ($word) {
            return strpos($word, $qword) !== false;
        })) > 0;
    });
}, $results_array);

Output print_r($result);:

Array
(
    [0] => Array
        (
            [5] => css
            [6] => lamp
        )

    [1] => Array
        (
            [1] => keyboard
            [2] => laptop
            [3] => pad
        )

    [2] => Array
        (
            [0] => table
            [1] => sofa
            [2] => caption
        )

    [3] => Array
        (
            [0] => control
            [2] => control television
            [3] => television control
        )

)
Carlos
  • 4,949
  • 2
  • 20
  • 37
  • set q= 'tele co', pls read my question complete, this is what I already done, the problem is with queries which have space in them, plz see my question last part, thanks – behz4d Nov 14 '12 at 18:00
  • 1
    @behz4d Done! Just 9 lines :) – Carlos Nov 14 '12 at 20:27
1

If I understand your question correctly you're looking for this:

$result = array();
$parts = explode( ' ', $q);
foreach($results_array as $words){
     foreach( explode(',', $words) as $word){
         $good = true;

         foreach( $parts as $p){
             if( strpos( $word, $p) === false){
                 $good = false;
                 break;
             }
         }

         if( $good){
            $result[] = $word;
         }
     }
}
Vyktor
  • 20,559
  • 6
  • 64
  • 96