0

I have an associated array,

$array2 = array(
    array('customer_id' => '01', 'categories' => '',),

     array('customer_id' => '02', 'categories' => '',),

     array('customer_id' => '03', 'categories' => '20,26,18',),

     array('customer_id' => '04', 'categories' => '45,118',),
);

I need to fetch the arrays with category value 18. I have exploded the category values and checked them in array_filter.

function testCategories($var)
{
    $like = 18;
    $categoryArray = array_column($var, 'categories');
    foreach($categoryArray as $ca){
    $caEplode = explode(',', $ca);
        foreach($caEplode as $cae){

            return($cae == $like);
        }
    }
}

print_r(array_filter($array2,"testCategories"));

But I am returning an empty string. Can any one please help me?

Syscall
  • 19,327
  • 10
  • 37
  • 52
hakkim
  • 666
  • 15
  • 36

3 Answers3

3

You don't have to use array_column() because, inside the callback of array_filter(), the categories is a key of the array in parameter. You could use in_array() with the exploded value of categories.

$array2 = array(
    array('customer_id' => '01', 'categories' => '',),
    array('customer_id' => '02', 'categories' => '',),
    array('customer_id' => '03', 'categories' => '20,26,18',),
    array('customer_id' => '04', 'categories' => '45,118',),
);

function testCategories($var)
{
    $like = 18 ;
    $caEplode = explode(',', $var['categories']);
    return in_array($like, $caEplode) ;
}
print_r(array_filter($array2,"testCategories"));

Output:

Array
(
    [2] => Array
        (
            [customer_id] => 03
            [categories] => 20,26,18
        )

)

You also could use an anonymous function and let $like outside the function:

$like = 18;
$out = array_filter($array2, function ($var) use ($like) {
    $caEplode = explode(',', $var['categories']);
    return in_array($like, $caEplode) ;
});
print_r($out);
Syscall
  • 19,327
  • 10
  • 37
  • 52
1

I didn't use array_filter but this might be fine

<?php
    $array2 = array(
        array('customer_id' => '01', 'categories' => '',),

         array('customer_id' => '02', 'categories' => '',),

         array('customer_id' => '03', 'categories' => '20,26,18',),

         array('customer_id' => '04', 'categories' => '45,118',),
    );

    function testCategories($var)
    {
        $like = 18;
        foreach($var as $ca){
            $caEplode = explode(',', $ca['categories']);
            foreach($caEplode as $cae){
                if($cae == $like)
                  $result[] = $ca;
            }
        }
        return $result;
    }

    print_r(testCategories($array2));


     ?>

result

Array ( [0] => Array ( [customer_id] => 03 [categories] => 20,26,18 ) )
Riccardo
  • 346
  • 2
  • 17
0

You can use preg_match with \b to make sure it only matches full words of "18".

$like = 18;
foreach($array2 as $val){
    if(preg_match("/\b" . $like . "\b/", $val['categories'])){
         $res[] = $val;
    }
}

var_dump($res);

https://3v4l.org/lBKVh


Another method is to use preg_grep on the categories column and use array_intersect_key to get the matching arrays.
This means no looping is needed at all.

$like = 18;
$cat = array_column($array2, 'categories');
$match = preg_grep("/\b" . $like . "\b/", $cat);
$res = array_intersect_key($array2, $match);

var_dump($res);

https://3v4l.org/tH0n1 I don't think there will be any or much difference between this method and array_filter.

It can also be written as a one liner, harder to read but with a comment it makes a short and concise.

$res = array_intersect_key($array2, preg_grep("/\b" . $like . "\b/", array_column($array2, 'categories')));
Andreas
  • 23,610
  • 6
  • 30
  • 62