एक रेगेक्स-आधारित समाधान इस प्रश्न के लिए उपयुक्त लगता है।
preg_grep() एक फ़ंक्शन है जो किसी सरणी में प्रत्येक मान पर रेगेक्स फ़िल्टर लागू करने के लिए डिज़ाइन किया गया है। इस मामले के लिए मैं थोड़ा और tweaking आवश्यक है क्योंकि चाबियाँ इसके बजाय फ़िल्टर किया जाना चाहिए।
एक-लाइनर:
$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,
)*/
तुम्हें पता है कि सरणी में कुंजी भिन्न क्रम में हो सकता है ...? –
हो सकता है कि आपको केवल 'NUMBER_1' कुंजी वाले मानों की आवश्यकता हो? –
यदि NUMBER_1 संभव है तो यह सही समाधान होगा। –