简体   繁体   English

基于键范围的PHP SPLIT数组

[英]PHP SPLIT array based on key range

I've an array. 我有一个阵列。

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
    )

Now I want to split all element upper range key is "project_shortcode_1" and lower range key is remarks_1. 现在我想拆分所有元素的上限范围键是“project_shortcode_1”,而下限范围键是remarks_1。

So, new array should look like: 所以,新数组应该如下所示:

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

Use array_filter with flag ARRAY_FILTER_USE_KEY for using the array keys, and do the comparison with the logic needed to get the desired keys. 使用带有标志ARRAY_FILTER_USE_KEY来使用数组键,并与获取所需键所需的逻辑进行比较。 It works from PHP 5.6. 它适用于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);

If what you need is a multidimensional array with all the ranges NUMBER_N , then use something like this (extended from Dmitriy Demir answer): 如果你需要的是一个包含所有范围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));

The result will be something like: 结果将是这样的:

    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
        )

)

being N from NUMBER_N the index of the array. NNUMBER_N阵列的索引。

Since you mentioned in the comments that you'd prefer to get all values that are in format NUMBER_1 I think you'd need to loop through your array and check the value names with regex, then add the values to a new array if they meet the criteria. 由于您在评论中提到您希望获得格式为NUMBER_1所有值,我认为您需要遍历数组并使用regex检查值名称,然后将值添加到新数组中标准。 Here's how I would do this: 这是我将如何做到这一点:

$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);

The result of print_r in that case would be: 在这种情况下, print_r的结果将是:

Array ( [5_1] => 23 [6_1] => 3.3 [4_1] => 23.2 [5_2] => 1.1 [6_2] => 2.2 [4_2] => 3.1 ) 数组([5_1] => 23 [6_1] => 3.3 [4_1] => 23.2 [5_2] => 1.1 [6_2] => 2.2 [4_2] => 3.1)

If the number after the underscore should always be 1 then change the regex from /^\\d+_\\d+$/ to /^\\d+_1$/ . 如果下划线后的数字应始终为1,则将正则表达式从/^\\d+_\\d+$/更改为/^\\d+_1$/

You can play around and see how regex works here . 你可以玩,看看正则表达式如何在这里工作

PS: I've set all values to strings out of convenience. PS:我已将所有值设置为字符串以方便使用。 Feel free to modify that. 随意修改它。

A regex-based solution seems fitting for this question. 基于正则表达式的解决方案似乎适合这个问题。

preg_grep() is a function designed to apply a regex filter upon each value in an array. preg_grep()是一个设计用于对数组中的每个值应用正则表达式过滤器的函数。 I little more tweaking is necessary for this case because the keys must be filtered instead. 对于这种情况,我需要进行更多调整,因为必须过滤密钥。

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,
    )*/

Here is the step-by-step array manipulation... 这是一步一步的数组操作......

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,
   )*/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM