基于密钥范围的PHP分割数组

时间:2021-12-21 03:19:06

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”,下量程键则是娱令1。

So, new array should look like:

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


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

4 个解决方案

#1


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. It works from PHP 5.6.

使用带有标志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);

#2


2  

If what you need is a multidimensional array with all the ranges NUMBER_N, then use something like this (extended from Dmitriy Demir answer):

如果您需要的是一个具有所有range 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.

N来自NUMBER_N是数组的索引。

#3


1  

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. Here's how I would do this:

由于您在评论中提到,您希望获得格式为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);

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)= 1.1 > 23.2(5 _2)= >[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.

您可以在这里玩一玩,看看regex是如何工作的。

PS: I've set all values to strings out of convenience. Feel free to modify that.

PS:为了方便,我把所有的值都设置成字符串。请随意修改。

#4


1  

A regex-based solution seems fitting for this question.

基于regex的解决方案似乎适合这个问题。

preg_grep() is a function designed to apply a regex filter upon each value in an array. I little more tweaking is necessary for this case because the keys must be filtered instead.

preg_grep()是设计用来对数组中的每个值应用regex过滤器的函数。对于这种情况,我只需稍作调整,因为键必须被过滤。

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

#1


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. It works from PHP 5.6.

使用带有标志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);

#2


2  

If what you need is a multidimensional array with all the ranges NUMBER_N, then use something like this (extended from Dmitriy Demir answer):

如果您需要的是一个具有所有range 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.

N来自NUMBER_N是数组的索引。

#3


1  

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. Here's how I would do this:

由于您在评论中提到,您希望获得格式为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);

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)= 1.1 > 23.2(5 _2)= >[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.

您可以在这里玩一玩,看看regex是如何工作的。

PS: I've set all values to strings out of convenience. Feel free to modify that.

PS:为了方便,我把所有的值都设置成字符串。请随意修改。

#4


1  

A regex-based solution seems fitting for this question.

基于regex的解决方案似乎适合这个问题。

preg_grep() is a function designed to apply a regex filter upon each value in an array. I little more tweaking is necessary for this case because the keys must be filtered instead.

preg_grep()是设计用来对数组中的每个值应用regex过滤器的函数。对于这种情况,我只需稍作调整,因为键必须被过滤。

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