通过匹配关键字按数组索引自定义php数组

时间:2018-03-15 03:43:04

标签: php

我有一个像下面这样的php数组。

$array1 =Array
            (
                [date_stamp] => 31/01/2018
                [over_time_policy_id] => 3
                [over_time] => 04:00
                [over_time_policy-2] => 02:00 //this
                [schedule_working] => 00:00
                [schedule_absence] => 00:00
                [shedule_start_time] => 
                [shedule_end_time] => 
                [min_punch_time_stamp] => 8:00
                [max_punch_time_stamp] => 20:00
                [regular_time] => 01:00
                [over_time_policy-3] => 02:00  //this
                [worked_time] => 12:00
                [actual_time] => 12:00
                [actual_time_diff] => 00:00
                [actual_time_diff_wage] => 0.00
                [hourly_wage] => 47.1200
                [paid_time] => 12:00
            )

我想从数组索引中获得下面的结果。

$result = Array (
    [0]=>over_time_policy-2
    [1]=>over_time_policy-3
);

我实现了以下代码,但没有输出任何内容。

$searchword = 'over_time_policy-';
foreach (preg_grep('/\b$searchword\b/i', $array1) as $key => $value) {
     print_r($key);
}

请帮我解决这个问题。

4 个答案:

答案 0 :(得分:3)

你可以这样做:

$searchword = 'over_time_policy-';
$output = [];
foreach ($array1 as $key => $value) {
    if (preg_match('/'.$searchword.'/i', $key)) {
        $output[] = $key;
    }
}

print_r($output);

答案 1 :(得分:3)

这可能对您有所帮助。 (获取数组键(array_keys),然后在其上应用preg_match

       $array1 = [
            'date_stamp' => '31/01/2018',
            'over_time_policy_id' => 3,
            'over_time' => '04:00',
            'over_time_policy-2' => '02:00', //this
            'schedule_working' => '00:00',
            'schedule_absence' => '00:00',
            'shedule_start_time' => '',
            'shedule_end_time' => '',
            'min_punch_time_stamp' => '8:00',
            'max_punch_time_stamp' => '20:00',
            'regular_time' => '01:00',
            'over_time_policy-3' => '02:00',  //this
            'worked_time' => '12:00',
            'actual_time' => '12:00',
            'actual_time_diff' => '00:00',
            'actual_time_diff_wage' => '0.00',
            'hourly_wage' => '47.1200',
            'paid_time' => '12:00'
        ];

        $searchword = 'over_time_policy-';
        $output = [];
        foreach (array_keys($array1) as $k) {
            if (preg_match("/\b$searchword\b/i", $k)) {
                $output[] = $k;
            }
        }
        echo "<br>";
        print_r($output);

输出: -

Array ( [0] => over_time_policy-2 [1] => over_time_policy-3 )

答案 2 :(得分:1)

我想出了如下答案,

$searchword = 'over_time_policy-';
    $matches = array();
    foreach($array1 as $k=>$v) {
        if(preg_match("/\b$searchword\b/i", $k)) {
            $matches[] = $k;
        }
    }
    print_r($matches);

我给了我下面的数组

$matches= Array (
    [0]=>over_time_policy-2
    [1]=>over_time_policy-3
);

答案 3 :(得分:1)

这对答案进行了一点编辑

    $word = 'over_time_policy-';
    $output = [];
    foreach (array_keys($array1) as $k) {
        if (preg_match("/\b$word\b/i", $k)) {
            $output[] = $k;
        }
    }

    print_r($output);