过滤并计算过滤后的数组

时间:2019-07-14 03:32:56

标签: php array-filter array-intersect preg-grep

我正在尝试过滤多维数组并返回为给定类别找到的条目数。

编辑

我正在尝试避免使用foreach。目前正在考虑使用array_intersect,但遇到数组到字符串的转换错误。我正在这样做:

//filter array with category name
$category_array = array('T-Shirt','Men');
$filter_category = (array_intersect($products['result'], $category_array));
var_export($filter_category);//return data of filtered entries
echo $row_count=count($filter_category);//return number of entries

所需结果应该返回与下面完全相同的数组,但仅包含过滤后的条目。如果有更好的方法来做我想做的事情。我打开了!

这是未过滤的数据

$products={
        "code": 200,
        "result": [
            {
                "id": 121935189,
                "external_id": "5d27b7962ac2c1",
                "name": "RPROD7742019 Dark Green Bush Camo Men's T-Shirt ",
                "variants": 6,
                "synced": 6
            },
            {
                "id": 121934788,
                "external_id": "5d27b641b1f568",
                "name": "RPROD2282019 FOREST GREEN CAMO Men's T-shirt",
                "variants": 6,
                "synced": 6
            },
            {
                "id": 121934520,
                "external_id": "5d27b555048158",
                "name": "RPROD7542019 Men's Rash Guard T-Shirt Sweatshirt Long Sleeve",
                "variants": 7,
                "synced": 7
            },
            {
                "id": 121934241,
                "external_id": "5d27b467b817b4",
                "name": "RPROD4952019 Men's T-shirt",
                "variants": 6,
                "synced": 6
            },
            {
                "id": 121933767,
                "external_id": "5d27b34c3133a2",
                "name": "RPROD5872019 Men's T-shirt ",
                "variants": 6,
                "synced": 6
            },
            {
                "id": 121933319,
                "external_id": "5d27b12e253032",
                "name": "RPROD7662019 DARK GREEN CAMO Men's T-Shirt - Men",
                "variants": 6,
                "synced": 6
            },
            {
                "id": 121931543,
                "external_id": "5d27afd7c949b8",
                "name": "RPROD1192019 Jungle Camo Men's T-Shirt - Men",
                "variants": 6,
                "synced": 6
            },
            {
                "id": 121930567,
                "external_id": "5d27ae02dd00a8",
                "name": "RPROD7572019 Camo Short sleeve men’s t-shirt - Men",
                "variants": 6,
                "synced": 6
            },
            {
                "id": 121930354,
                "external_id": "5d27ad2a338399",
                "name": "RPROD8412019 Camo Men's T-shirt - Men",
                "variants": 6,
                "synced": 6
            },
            {
                "id": 121665999,
                "external_id": "5d251acde94bc7",
                "name": "RPROD3042019 Champion Dad Cap Trucker Cap Men",
                "variants": 2,
                "synced": 2
            },
            {
                "id": 121665890,
                "external_id": "5d251a50cb6a82",
                "name": "RPROD7162019 Vintage Cotton Twill Cap Trucker Cap Men Women",
                "variants": 3,
                "synced": 3
            },
            {
                "id": 121665703,
                "external_id": "5d2519cba9fea7",
                "name": "RPROD4872019 Dad hat Trucker Cap Men Women",
                "variants": 1,
                "synced": 1
            },
            {
                "id": 121665596,
                "external_id": "5d251972bd8b36",
                "name": "RPROD2492019 Dad hat Trucker Cap Men Women",
                "variants": 1,
                "synced": 1
            },
            {
                "id": 121665533,
                "external_id": "5d25193756ac83",
                "name": "RPROD5362019 Trucker Cap Dad hat Men Women",
                "variants": 5,
                "synced": 5
            },
            {
                "id": 121665382,
                "external_id": "5d2518ae661cf3",
                "name": "RPROD3082019 Cotton Cap Trucker Cap Dad Hat Men Women",
                "variants": 7,
                "synced": 7
            },
            {
                "id": 121665266,
                "external_id": "5d2518395f7b28",
                "name": "RPROD6372019 Trucker Cap Men Women",
                "variants": 1,
                "synced": 1
            },
            {
                "id": 121665141,
                "external_id": "5d2517dc928059",
                "name": "RPROD8112019 Trucker Cap Men Women",
                "variants": 9,
                "synced": 9
            },
            {
                "id": 121664869,
                "external_id": "5d25171b371fa4",
                "name": "RPROD7232019 Trucker Cap Men Women",
                "variants": 4,
                "synced": 4
            },
            {
                "id": 121664726,
                "external_id": "5d2516c175ad54",
                "name": "RPROD4112019 Trucker Cap Men Women",
                "variants": 5,
                "synced": 5
            },
            {
                "id": 121664599,
                "external_id": "5d251659e8a218",
                "name": "RPROD7002019 Trucker Cap Men Women",
                "variants": 1,
                "synced": 1
            },
            {
                "id": 121664477,
                "external_id": "5d251609908887",
                "name": "RPROD3232019 Trucker Cap Men Women",
                "variants": 5,
                "synced": 5
            },
            {
                "id": 121664346,
                "external_id": "5d2515ade45543",
                "name": "RPROD6702019 Trucker Cap - Men Women",
                "variants": 5,
                "synced": 5
            },
            {
                "id": 121664113,
                "external_id": "5d2515167e62e4",
                "name": "RPROD9422019 Cotton Cap Dad Hat Women Men",
                "variants": 8,
                "synced": 8
            },
            {
                "id": 121663700,
                "external_id": "5d2513fb5e6572",
                "name": "RPROD2552019 Cotton Trucker Cap Dad Hat Men Women",
                "variants": 4,
                "synced": 4
            },
            {
                "id": 121663350,
                "external_id": "5d251304231b28",
                "name": "RPROD2762019 Trucker Cap Men Women",
                "variants": 9,
                "synced": 9
            }
        ],
        "extra": [],
        "paging": {
            "total": 75,
            "offset": 0,
            "limit": 25
        }
    }

2 个答案:

答案 0 :(得分:1)

您可以为此使用array_filter。将类别数组转换为匹配所有单词的正则表达式;参见Regular Expression to MATCH ALL words in a query, in any order。然后在过滤器功能中使用preg_match()

$regexp = '/(?=.*\bt-shirt\b)(?=.*\bmen\b)/i';
$fl_list = array_filter(function($p) use ($regexp) {
    return preg_match($regexp, $p['name']);
}, $products['result']);

我也在常规表达中使用了\b,以使其与整个单词匹配。否则,men将与women相匹配。

如果您在数据中使用真实的关键字,而不是尝试匹配产品名称中的单词,那么事情会容易得多。

答案 1 :(得分:1)

类似这样的东西:

$category_array = array('T-Shirt', 'camo');

print_r(
  array_intersect_key(
      $products['result'],
      preg_grep(
          '/(?=.*\b'.implode('\b)(?=.*\b', $category_array).'\b)/i',
          array_column($products['result'], 'name')
      )
  )
);

输出:

Array
(
    [0] => Array
        (
            [id] => 121935189
            [external_id] => 5d27b7962ac2c1
            [name] => RPROD7742019 Dark Green Bush Camo Men's T-Shirt 
            [variants] => 6
            [synced] => 6
        )

    [1] => Array
        (
            [id] => 121934788
            [external_id] => 5d27b641b1f568
            [name] => RPROD2282019 FOREST GREEN CAMO Men's T-shirt
            [variants] => 6
            [synced] => 6
        )

    [5] => Array
        (
            [id] => 121933319
            [external_id] => 5d27b12e253032
            [name] => RPROD7662019 DARK GREEN CAMO Men's T-Shirt - Men
            [variants] => 6
            [synced] => 6
        )

    [6] => Array
        (
            [id] => 121931543
            [external_id] => 5d27afd7c949b8
            [name] => RPROD1192019 Jungle Camo Men's T-Shirt - Men
            [variants] => 6
            [synced] => 6
        )

    [7] => Array
        (
            [id] => 121930567
            [external_id] => 5d27ae02dd00a8
            [name] => RPROD7572019 Camo Short sleeve men’s t-shirt - Men
            [variants] => 6
            [synced] => 6
        )

    [8] => Array
        (
            [id] => 121930354
            [external_id] => 5d27ad2a338399
            [name] => RPROD8412019 Camo Men's T-shirt - Men
            [variants] => 6
            [synced] => 6
        )

)

Sandbox

它们都包含“ Men”,所以我不确定过滤规则应该是什么。无论如何,经过一些调整,我改用了“ camo”。

PS,如果要更新阵列,则只需执行此操作即可代替print_r

$products['result'] = array_intersect_key( ... other code ... );

如果您也想要我,我可以解释它的工作原理。