0

我有一个关联的数组,

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

我需要获取类别值为 18 的数组。我已经分解了类别值并将它们签入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"));

但我返回一个空字符串。谁能帮帮我吗?

4

3 回答 3

3

您不必使用array_column(),因为在 的回调中array_filter()categories是参数中数组的键。您可以使用in_array()的爆炸值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"));

输出:

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

)

您还可以使用匿名函数并$like在函数之外使用:

$like = 18;
$out = array_filter($array2, function ($var) use ($like) {
    $caEplode = explode(',', $var['categories']);
    return in_array($like, $caEplode) ;
});
print_r($out);
于 2018-05-03T10:23:25.813 回答
1

我没有使用 array_filter 但这可能没问题

<?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));


     ?>

结果

Array ( [0] => Array ( [customer_id] => 03 [categories] => 20,26,18 ) )
于 2018-05-03T10:20:07.493 回答
0

您可以将 preg_match 与 \b 一起使用,以确保它仅匹配“18”的完整单词。

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

var_dump($res);

https://3v4l.org/lBKVh


另一种方法是在类别列上使用 preg_grep 并使用 array_intersect_key 来获取匹配的数组。
这意味着根本不需要循环。

$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 我不认为这种方法和array_filter 之间会有任何或很大的区别。

它也可以写成一个衬里,更难阅读,但带有评论,它会变得简短而简洁。

$res = array_intersect_key($array2, preg_grep("/\b" . $like . "\b/", array_column($array2, 'categories')));
于 2018-05-03T10:23:02.993 回答