过滤并计算过滤后的数组

filter and count filtered array

我正在尝试过滤一个多维数组和 return 为给定类别找到的条目数。

编辑

我试图避免使用 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

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

这是数据未经过滤的样子

$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
        }
    }

您可以为此使用 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.

如果您的数据中有真正的关键词,而不是尝试匹配产品名称中的词,事情会容易得多。

像这样:

$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 ... );

如果你也需要我,我可以解释它是如何工作的。