简体   繁体   中英

Get count of regex matches in array

Fairly new to php so forgive me if this is a trivial question.

I'm creating an array based on a directory containing images with a few different naming conventions. Here's some sample code of the array construction:

<?php
    $path = '../regions'; //path contains child directories north/, west/, south/, etc.
        //each of these child directories contains images listed in Array below

    $regions = array_flip(array_diff(scandir($path), array('.', '..')));
        // $regions = Array([north] => [2], [west] => [3], ..., [south] => [6])

    foreach ($regions as $key => $value) {
        $images = array_diff(scandir($path.'/'.$key.'/'.$regionkey), array('.', '..'));
        $regions[$key] = $images;
            //$regions is now the Array shown in code section below
    }

?>

The array produced by the code above looks roughly like this:

[north] => Array(
    [2] => windprod_f1.png
    [3] => windprod_f2.png
    ...
    [20] => windprod_f18.png
    [21] => temp_sim_f1.png
    [22] => temp_sim_f2.png
    ...
    [36] => temp_sim_f16.png
    [37] => pres_surf_f1.png
    [38] => pres_surf_f2.png
    [45] => pres_surf_f9.png
    ...
)
[south] => Array (
    [2] => windprod_f1.png
    [3] => windprod_f2.png
    ...
    [20] => windprod_f18.png
    [21] => temp_sim_f1.png
    [22] => temp_sim_f2.png
    ...
    [32] => temp_sim_f12.png
    [33] => pres_surf_f1.png
    [34] => pres_surf_f2.png
    ...
    [58] => pres_surf_f24.png
    ....
)
...

There are 5 unique file naming conventions (windprod, temp_sim, pres_surf, etc.), each of which have some varying number of images associated with them (_f1, _f2, ..., f_18, etc.). After constructing the array as I have done, I need to get the count of images for each specific file naming convention. Ideally, I would like the $key to be the product name (the substring preceding _f(\\d{1,2}).png in each filename), and the $value to be the number of files containing that specific substring in the array.

Ie, my final array has to look like this:

[north] => Array (
    [windprod] => 18 //$key = regex match, $values = number of matches in Array
    [temp_sim] => 16
    [pres_surf] => 9
    ...
    )
[south] => Array (
    [windprod] => 18
    [temp_sim] => 12
    [pres_surf] => 24
    ...
    )
...

Anybody have any ideas here?

Thanks to all in advance.

Simple iteration should work fine I think, something like this

foreach ($regions as $region => $images) {
    $result = [];
    foreach ($images as $image) {
        $type = preg_replace('/_f\d+\.png$/', '', $image);
        if (!array_key_exists($type, $result)) {
            $result[$type] = 0;
        }
        $result[$type]++;
    }
    $regions[$region] = $result;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM