[英]PHP SPLIT array based on key range
我有一個陣列。
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 )
使用帶有標志ARRAY_FILTER_USE_KEY
來使用數組鍵,並與獲取所需鍵所需的邏輯進行比較。 它適用於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);
如果你需要的是一個包含所有范圍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
陣列的索引。
由於您在評論中提到您希望獲得格式為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$/
。
你可以玩,看看正則表達式如何在這里工作 。
PS:我已將所有值設置為字符串以方便使用。 隨意修改它。
基於正則表達式的解決方案似乎適合這個問題。
preg_grep()是一個設計用於對數組中的每個值應用正則表達式過濾器的函數。 對於這種情況,我需要進行更多調整,因為必須過濾密鑰。
單線 :
$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,
)*/
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.