1

I have a problem and i'm not sure how to fix it.

It's quite different from anything I have done before.

I got this array:

array (
'DayTime_s' => '12:38',
'MATCHTIMEOUT_f' => '120.000000',
'MOD0_s' => '746138492',
'MOD1_s' => '841244723',
'MOD2_s' => '741155722',
'MOD3_s' => '748747466',
'ModId_l' => '0',
'Networking_i' => '1',
'NUMOPENPUBCONN' => '40',
)

The size of the array is different from one array to the next. What I need is a new array, containing all "MOD?_s" entries above (with values). But the number of "mods" is different from one array to another. The numbers always increment by one though, starting from 0 (0,1,2,3,4 etc.)

How should I handle this task?

, Kenneth

0

2 Answers 2

4
$your_array = array (
'DayTime_s' => '12:38',
'MATCHTIMEOUT_f' => '120.000000',
'MOD0_s' => '746138492',
'MOD1_s' => '841244723',
'MOD2_s' => '741155722',
'MOD3_s' => '748747466',
'ModId_l' => '0',
'Networking_i' => '1',
'NUMOPENPUBCONN' => '40',
);

$new_array = [];
foreach($your_array as $key => $val)
    if(preg_match('/MOD\d+_s/ui', $key))
        $new_array[$key] = $val;

print_r($new_array);
Sign up to request clarification or add additional context in comments.

1 Comment

That was fast. Already tested it and it works flawless! Thanks a lot :)
0

Late answer, but you can also use array_keys() and unset() :

foreach( array_keys($the_array) as $key)
{
    if (!preg_match('/MOD\d+/s', $key)) {
        unset($the_array[$key]);
    }
}

print_r($test_array);

Output:

Array
(
    [MOD0_s] => 746138492
    [MOD1_s] => 841244723
    [MOD2_s] => 741155722
    [MOD3_s] => 748747466
)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.