-1

I'm trying to do some predictive searching, and I am using preg_grep() as a way to get away from the LIKE in SQL.

I have the following array:

$values = array("Phorce" => 123, "Billy" => 234);

If I have Phorc I want all array elements (key and value), with a partial match of Phorc, so here Phorce => 123. Is this possible with preg_grep() ?

I have tried to use the following:

$result = preg_grep('~' . 'Phorce' . '~', $values);

How can I get my code to work as described above?

mickmackusa
  • 43,625
  • 12
  • 83
  • 136
Phorce
  • 4,424
  • 13
  • 57
  • 107
  • Add the code you have tried. – Sougata Bose Jun 01 '15 at 12:57
  • Let me better understand what you're doing. If a table contains a 1,000 rows, you will get them all ... and then filter them in PHP, rather than filtering them with SQL? – Jonathan Jun 01 '15 at 12:58
  • @Augwa I have updated this. – Phorce Jun 01 '15 at 12:59
  • @Phorce that doesn't answer my question. Why are you trying to avoid the LIKE in SQL? – Jonathan Jun 01 '15 at 13:01
  • @Augwa Instead of using SQL. I have an array of values (names) and each of these values have an id so `Phorce => 1234` what I want to do is, search through this array for all the possibilities that contain for example `Phor` so if someone passed in this value, it would then return `Phorce` – Phorce Jun 01 '15 at 13:02
  • Duplicate that is only missing the `ARRAY_FILTER_USE_KEY` flag. https://stackoverflow.com/questions/6932438/search-for-partial-value-match-in-an-array – mickmackusa May 11 '21 at 10:00

3 Answers3

1

This should work for you:

Here I just use array_filter() combined with stripos() to search through each array key if it contains the search value.

<?php

    $array = array("Phorce" => 123, "Billy" => 234);

    $search = "Phor";
    $arr = array_filter($array, function($k)use($search){
        return stripos($k, $search) !== FALSE;
    }, ARRAY_FILTER_USE_KEY);

    print_r($arr);

?>

output:

Array
(
    [Phorce] => 123
) 

Before someone says, that it doesn't work: You need PHP 5.6, so if you don't have that upgrade!

Rizier123
  • 58,877
  • 16
  • 101
  • 156
  • I haven't been able to find a dupe to close with. Perhaps update your answer to mention `str_contains()` in PHP8. – mickmackusa Jan 18 '22 at 02:31
1

So you want to search the keys then?

$values = array("Phorce" => 123, "Billy" => 234);
$results = preg_grep('/Phor/', array_keys($values));

$arr = [];
foreach($results as $result) {
    $arr[$result] = $values[$result];
}

print_r($arr);
Jonathan
  • 2,778
  • 13
  • 23
  • Thank you! This works :)! The only problem is, I want to return the key values as well as the keys? .. This seems to only give the keys and not the values – Phorce Jun 01 '15 at 13:11
  • @Rizier123 answer does just that, but I'll update mine to make it it include them as well. – Jonathan Jun 01 '15 at 13:13
  • Too many loops in this answer. Use Rizier's non-regex filter. – mickmackusa May 11 '21 at 09:59
0

Use Below code to get Response:

Demo Code

<?php
$values = array("Phorce" => 123, "Billy" => 234);

var_export(
    array_filter(
        $values,
        function($haystack ) {
          if (is_numeric(strpos($haystack, 'Phorce'))) {
            return $haystack;
          } 
        },
        ARRAY_FILTER_USE_KEY
    )
);
Swadesh Ranjan Dash
  • 544
  • 1
  • 4
  • 17