简体   繁体   English

从包含特定字符串的数组创建数组

[英]create arrays from array containg specific strings

I have a desc array created from a scandir(). 我有一个从scandir()创建的desc数组。 Here is a small example of the array: 这是数组的一个小示例:

[7] => 20151227NY [8] => 20151226NY [9] => 20151221NY [10] => 20151125CT [11] => 20140313NY [12] => 20140228NY [13] => 20140227NY [14] => 20140226NH [15] => 20140226CT [16] => 20140128NJ [17] => 20140123NY [18] => 20140122CT [19] => 20140121NJ [20] => 20140102NY [21] => 20131231NJ 

These are all folders named using YearMonthDateState. 这些都是使用YearMonthDateState命名的文件夹。 I need a way to grab all the folders containing "NY", "CT", "NJ", "NH" and put them in separate arrays that sort in desc order(latest date first). 我需要一种方法来抓取所有包含“ NY”,“ CT”,“ NJ”,“ NH”的文件夹,并将它们放在以desc顺序排序的单独数组中(最新日期在前)。 That way I can get files I need in them according to the latest date(which is in the folder name). 这样,我可以根据最新日期(在文件夹名称中)获取其中需要的文件。 I tried this: 我尝试了这个:

//GET WEEKLY FOLDER
$weekly_dir = "../email/weekly/";
$weekly_files = scandir($weekly_dir, $sorting_order = 1);
$weekly_index =  $weekly_files[7];
$weekly_state = substr($weekly_index, 8,2);
$new_weekly = str_replace($weekly_state, $template->State, $weekly_index);
echo $new_weekly;

But unfortunately, not every new "weekly" is uploaded on the same date. 但不幸的是,并非每个新的“每周”都在同一日期上传。 So I cant just switch the state string ("NY" "NH" etc) at the end. 所以我不能只在末尾切换状态字符串(“ NY”,“ NH”等)。 Any ideas? 有任何想法吗?

**UPDATE:**I have added/modified the code as suggested by Michael so my code now looks like this: **更新:**我已经按照迈克尔的建议添加/修改了代码,所以我的代码现在看起来像这样:

$weekly_dir = "../email/weekly/";
$weekly_file_array = scandir($weekly_dir, $sorting_order = 1);
$index = $weekly_file_array;

// Prepare result
$weeklies = array_fill_keys(array_map(function ($item) {
if (substr($item, -2) == 'NY'){
    return substr($item, -2);
}
}, $index),$item);

// Split into state arrays
array_walk($index, function ($item) use (&$weeklies) {
    $state = substr($item, -2);
    $weeklies[$state][] = $item;
});

$weeklies_latest = array_combine(array_keys($weeklies), array_map(function ($item) {
    sort($item);
    return array_pop($item);
 }, $weeklies));

Seems to work just fine, but I get these two errors at the top of page: "Warning: sort() expects parameter 1 to be array, null given" "Warning: array_pop() expects parameter 1 to be array, null given". 似乎可以正常工作,但是在页面顶部出现了两个错误:“警告:sort()期望参数1为数组,给定null”“警告:array_pop()期望参数1为数组,给定null” 。 I am calling the $weeklies_latest array like this: 我正在这样调用$ weeklies_latest数组:

<a href="/COMM/email/weekly/<? echo $weeklies_latest[$template->State] ?>/weekly.html" class="button-link button-link-blue">CURRENT ISSUE</a>

I usually do it in two steps because it makes the code more readable. 我通常分两步进行操作,因为它使代码更具可读性。

First I prepare the result based on states, using a combination of array_fill_keys and array_map 首先,结合使用array_fill_keysarray_map ,根据状态准备结果

// Prepare result
$weeklies = array_fill_keys(array_map(function ($item) {
    return substr($item, -2);
}, $input), []);

This builds an array with states as keys pointing to empty arrays. 这将建立一个状态为指向空数组的键的数组。 In the next step I once again "walk" through all the elements and then add them to the propper state array. 在下一步中,我再次“遍历”所有元素,然后将它们添加到propper状态数组。

// Split into state arrays
array_walk($input, function ($item) use (&$weeklies) {
    $state = substr($item, -2);
    $weeklies[$state][] = $item;
});

(you see how this could just as well have been done in one step? you can use foreach instead of array_walk if you prefer that) (您知道一步就能完成吗?如果愿意,可以使用foreach代替array_walk

Now you are left with something like this: 现在您剩下的是这样的:

Array
(
    [NY] => Array
        (
            [0] => 20151227NY
            [1] => 20151226NY
            [2] => 20151221NY
            [3] => 20140313NY
            [4] => 20140228NY
            [5] => 20140227NY
            [6] => 20140123NY
            [7] => 20140102NY
        )

    [CT] => Array
        (
            [0] => 20151125CT
            [1] => 20140226CT
            [2] => 20140122CT
        )

    [NH] => Array
        (
            [0] => 20140226NH
        )

    [NJ] => Array
        (
            [0] => 20140128NJ
            [1] => 20140121NJ
            [2] => 20131231NJ
        )

)

To pick the latest for each state you'll need to sort your dates since there is not guarantee of their order from the filesystem (it seems from your data). 要选择每个州的最新状态,您需要对日期进行排序,因为不能保证它们在文件系统中的顺序(从您的数据来看)。 After sorting we pick the one at the end. 排序后,我们在最后选择一个。 I'm using array_pop since you might have a week where one day failed or whatnot, so it is better not to rely on perfect data: 我正在使用array_pop因为您可能有一个星期可能有一天失败了,所以最好不要依赖完美的数据:

$weeklies_latest = array_combine(array_keys($weeklies), array_map(function ($item) {
    sort($item);
    return array_pop($item);
 }, $weeklies));

I'm using array_combine because PHP doesn't have a built-in array_map that works on associative arrays. 我之所以使用array_combine是因为PHP没有可在关联数组上使用的内置array_map

$weekly_latest now contains this: $weekly_latest现在包含以下内容:

Array
(
    [NY] => 20151227NY
    [CT] => 20151125CT
    [NH] => 20140226NH
    [NJ] => 20140128NJ
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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