Array of PHP SPLIT based on key range

I have 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 the upper keys of the upper level: "project_shortcode_1", and the lower key of the range - notes_1.

So, the new array should look like this:

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

+6
source share
4 answers

Use array_filter with the ARRAY_FILTER_USE_KEY flag to use array keys and compare with the logic needed to get the desired keys. It works with 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
source

If you need a multidimensional array with all ranges of NUMBER_N , then use something like this (extended from 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 look something like this:

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

is N of NUMBER_N array index.

+2
source

Since you mentioned in the comments that you would prefer to get all the values ​​that are in NUMBER_1 format, I think you will need to skip your array and check the value names with a regular expression, and then add the values ​​to the new array if they meet the criteria. Here's how I would do it:

 $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 this case will be:

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

If there should always be 1 after underscore, change the regular expression from /^\d+_\d+$/ to /^\d+_1$/ .

You can play and see how regex works here .

PS: I chose all the values ​​for the strings for convenience. Feel free to modify this.

+1
source

A regex-based solution seems appropriate for this question.

preg_grep () is a function designed to apply a regular expression filter to each value in an array. For this case, a little more settings are required, because instead you need to filter the keys.

Single line :

 $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 stepwise manipulation of arrays ...

 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
source

Source: https://habr.com/ru/post/1012498/


All Articles