1

I'm very new to PHP so any help here is greatly appreciated. I have an array that I need to search and return partial and exact matches. Here's the array and where I've started code-wise:

$employeeList = array(
array(
    'action' => false,
    'title' => '2015 Performance Appraisal - Basille, Victor P.',
    'type' => 'Employee Assessment',
    'status' => 'Evaluate',
    'date' => 'Dec 12, 2015',
    'actions' => array('Start','Delete','View Progress','Manage Rater and Peers','Print'),
    'employeeName' => 'Basille, Victor P.',
    'employeeProgress' => 0,
    'employeeStatus' => 'Not Yet Started',
    'raterName' => 'Crane, Davy L.',
    'raterProgress' => 0,
    'raterStatus' => 'Not Yet Started',
    'peers' => 3,
    'peersComplete' => 0
),
array(
    'action' => true,
    'title' => '2015 Performance Appraisal - Bradford, Julie D.',
    'type' => 'Employee Assessment',
    'status' => 'Evaluate',
    'date' => 'Dec 12, 2015',
    'actions' => array('Resume','Reset','View Progress','Delete','Manage Rater and Peers','Print'),
    'employeeName' => 'Bradford, Julie D.',
    'employeeProgress' => 10,
    'employeeStatus' => 'In Progress',
    'raterName' => 'Crane, Davy L.',
    'raterProgress' => 60,
    'raterStatus' => 'In Progress',
    'peers' => 3,
    'peersComplete' => 1
),
array(
    'action' => false,
    'title' => '2015 Performance Appraisal - Fiedler, Joanne M.',
    'type' => 'Employee Assessment',
    'status' => 'Review',
    'date' => 'Dec 12, 2015',
    'actions' => array('View','Reset','Revert','View Progress','Print'),
    'employeeName' => 'Fiedler, Joanne M.',
    'employeeProgress' => 100,
    'employeeStatus' => 'Submitted',
    'raterName' => 'Fay, Lester P.',
    'raterProgress' => 100,
    'raterStatus' => 'Submitted',
    'peers' => 3,
    'peersComplete' => 3
)
);

$search_term = 'Victor';

$key = array_search($search_term, array_column($employeeList, 'title'));

This works great, except it only works for exact matches of the 'title'. How can I make this return partial matches as well?

Thanks.

Marc B
  • 356,200
  • 43
  • 426
  • 500
Herb Himes
  • 41
  • 5
  • you can't. array_search() is exact match only. you'd need to walk the array and apply `strpos` or `preg_grep` – Marc B Oct 05 '15 at 14:50
  • Possible duplicate of [How to search in an array with preg\_match?](http://stackoverflow.com/questions/8627334/how-to-search-in-an-array-with-preg-match) – e-Learner Oct 05 '15 at 14:58

1 Answers1

0

what you need is preg_grep() which searchs the array for patern matching elements and returns them in an array. http://php.net/manual/en/function.preg-grep.php

$result_array = preg_grep("/{$keyword}/i",$employeeList)
Julio Soares
  • 1,200
  • 8
  • 11