从PHP

时间:2015-10-05 14:47:10

标签: php multidimensional-array

我是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'));

这很好用,除了它只适用于'标题'的完全匹配。我怎样才能让它返回部分匹配?

感谢。

1 个答案:

答案 0 :(得分:0)

你需要的是preg_grep(),它在数组中搜索patern匹配元素并将它们返回到数组中。 http://php.net/manual/en/function.preg-grep.php

$result_array = preg_grep("/{$keyword}/i",$employeeList)