基于键范围的PHP SPLIT数组

时间:2016-11-20 08:52:55

标签: php arrays

我有一个阵列。


    Array
    (
        [initial] => MSS
        [hour] => 5.2
        [row_checker_1] => 1
        [project_name_1] => KGD001
        [project_shortcode_1] => KGD001
        [5_1] => 23
        [6_1] => 3.3
        [4_1] => 23.2
        [remarks_1] =>  on going
        [task_id] => 76
        [row_checker_2] => 2
        [project_name_2] => DG001
        [project_shortcode_2] => DG001
        [5_2] => 1.1
        [6_2] => 2.2
        [4_2] => 3.1
        [remarks_2] =>   on going
    )

现在我要拆分所有元素上限范围键是“project_shortcode_1”,而下限范围键是remarks_1。

因此,新数组应如下所示:


    array
    (
        [5_1] => 23
        [6_1] => 3.3
        [4_1] => 23.2
    )

4 个答案:

答案 0 :(得分:2)

使用带有标志ARRAY_FILTER_USE_KEY的array_filter来使用数组键,并与获取所需键所需的逻辑进行比较。它适用于PHP 5.6。

$arr = array ( "initial" => "MSS",
        "hour" => 5.2,
        "row_checker_1" => 1,
        "project_name_1" => "KGD001",
        "project_shortcode_1" => "KGD001",
        "5_1" => 23,
        "6_1" => 3.3,
        "4_1" => 23.2,
        "remarks_1" =>  "on going",
        "task_id" => 76,
        "row_checker_2" => 2,
        "project_name_2" => "DG001",
        "project_shortcode_2" => "DG001",
        "5_2" => 1.1,
        "6_2" => 2.2,
        "4_2" => 3.1,
        "remarks_2" =>   "on going",
    );

// PHP > 5.6
$result = array_filter($arr, function($k){
    $var = explode('_', $k);
    return is_numeric($var[0]) && $var[1]==1;
}, ARRAY_FILTER_USE_KEY);

答案 1 :(得分:2)

如果你需要的是一个包含所有范围NUMBER_N的多维数组,那么使用类似这样的东西(从Dmitriy Demir回答扩展):

$myArray = array(
    'initial' => 'MSS',
    'hour' => '5.2',
    'row_checker_1' => '1',
    'project_name_1' => 'KGD001',
    'project_shortcode_1' => 'KGD001',
    '5_1' => '23',
    '6_1' => '3.3',
    '4_1' => '23.2',
    'remarks_1' => 'on going',
    'task_id' => '76',
    'row_checker_2' => '2',
    'project_name_2' => 'DG001',
    'project_shortcode_2' => 'DG001',
    '5_2' => '1.1',
    '6_2' => '2.2',
    '4_2' => '3.1',
    'remarks_2' => 'on going'
);

function splitRange($a){
    $newArray = array();
    foreach ($a as $k => $v) {
        $rightFormat = preg_match('/^\d+_(\d+)$/', $k, $index);
        if ($rightFormat)
            $newArray[$index[1]][$k] = $v;
    }
    return $newArray;
}

print_r(splitRange($myArray));

结果如下:

    Array
(
    [1] => Array
        (
            [5_1] => 23
            [6_1] => 3.3
            [4_1] => 23.2
        )

    [2] => Array
        (
            [5_2] => 1.1
            [6_2] => 2.2
            [4_2] => 3.1
        )

)

N数组索引NUMBER_N

答案 2 :(得分:1)

由于您在评论中提到您希望获得格式为NUMBER_1的所有值,我认为您需要遍历数组并使用regex检查值名称,然后添加值如果符合标准,则为新阵列。以下是我将如何做到这一点:

$myArray = array(
    'initial' => 'MSS',
    'hour' => '5.2',
    'row_checker_1' => '1',
    'project_name_1' => 'KGD001',
    'project_shortcode_1' => 'KGD001',
    '5_1' => '23',
    '6_1' => '3.3',
    '4_1' => '23.2',
    'remarks_1' => 'on going',
    'task_id' => '76',
    'row_checker_2' => '2',
    'project_name_2' => 'DG001',
    'project_shortcode_2' => 'DG001',
    '5_2' => '1.1',
    '6_2a' => '2.2',
    '4_2' => '3.1',
    'remarks_2' => 'on going'
);

$newArray = array();
foreach ($myArray as $k => $v) {
    $rightFormat = preg_match('/^\d+_\d+$/', $k);
    if ($rightFormat)
        $newArray[$k] = $v;
}
print_r($newArray);

在这种情况下,print_r的结果将是:

  

阵列([5_1] => 23 [6_1] => 3.3 [4_1] => 23.2 [5_2] => 1.1 [6_2] =>   2.2 [4_2] => 3.1)

如果下划线后的数字应始终为1,则将正则表达式从/^\d+_\d+$/更改为/^\d+_1$/

您可以四处游戏,了解正则表达式的工作原理here

PS:我已将所有值设置为字符串以方便使用。随意修改它。

答案 3 :(得分:1)

基于正则表达式的解决方案似乎适合这个问题。

preg_grep()是一个旨在对数组中的每个值应用正则表达式过滤器的函数。对于这种情况,我需要进行更多的调整,因为必须过滤密钥。

The One-liner

$output=array_intersect_key($input,array_flip(preg_grep("/^\d+_1$/",array_keys($input)))));
/* array (
      '5_1' => 23,
      '6_1' => 3.3,
      '4_1' => 23.2,
    )*/

以下是逐步数组操作......

array_keys($input);  // create array with input keys as values
/* array (
      0 => 'initial',
      1 => 'hour',
      2 => 'row_checker_1',
      3 => 'project_name_1',
      4 => 'project_shortcode_1',
      5 => '5_1',
      6 => '6_1',
      7 => '4_1',
      8 => 'remarks_1',
      9 => 'task_id',
      10 => 'row_checker_2',
      11 => 'project_name_2',
      12 => 'project_shortcode_2',
      13 => '5_2',
      14 => '6_2',
      15 => '4_2',
      16 => 'remarks_2',
   ) */

preg_grep("/^\d+_1$/",array_keys($input));  // filter the input array using regex pattern
/* array (
      5 => '5_1',
      6 => '6_1',
      7 => '4_1',
   ) */

array_flip(preg_grep("/^\d+_1$/",array_keys($input))); // flip the filtered array
/* array (
      '5_1' => 5,
      '6_1' => 6,
      '4_1' => 7,
   )*/

array_intersect_key($input,array_flip(preg_grep("/^\d+_1$/",array_keys($input))));  // filter input by comparing keys against filtered array
/* array (
      '5_1' => 23,
      '6_1' => 3.3,
      '4_1' => 23.2,
   )*/