phparraysfilteringwhitelistpreg-grep

Count rows in a 2d array which contain one of multiple search strings in a specific column


I'm trying to filter a multidimensional array and return the number of entries found for a given category.

EDIT

I'm trying to avoid any use of foreach. Right now looking into using array_intersect but getting array to string conversion error. I'm doing this :

//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

The desired result should return the exact same array seen below but only with filtered entries. If there is a better way to do what I'm trying to do. I'm opened!

This is what the data looks like unfiltered

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

Solution

  • Something like this:

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

    Output:

    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

    They all contain "Men" so I am not sure what the filter rules should be. Anyway, I used "camo" instead, after some tweaks.

    PS, if you want to update the array then just do this instead of print_r:

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

    I can explain how it works if you want me too.