我对PHP非常陌生,所以这里的任何帮助都是非常感谢的。我有一个数组,我需要搜索并返回部分匹配和精确匹配。这里是数组和我开始编写代码的地方:
$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'));
这个工作很棒,除了它只适用于'title'的精确匹配。我怎样才能使这个返回部分匹配?
谢谢。
您需要的是preg_grep()
,它搜索数组中的模式匹配元素并将它们返回到数组中。http://php.net/manual/en/function.preg-grep.php
$result_array = preg_grep("/{$keyword}/i",$employeeList)