0

我如何过滤此数组值[Name] =>尝试匹配值名称开头的单词“Good”

Array
(

[0] => Array
    (
        [Name] => Good Seat
        [image] => spot
        [category] => 0
    )

[1] => Array
    (
        [Name] => Nice Good Service
        [image] => spotless
        [category] => 0
    )

[2] => Array
    (
        [Name] => Good Work
        [image] => spotly
        [category] => 1
    )
)

我需要这样的过滤器

Array
(

[0] => Array
    (
        [Name] => Good Seat
        [image] => spot
        [category] => 0
    )

[1] => Array
    (
        [Name] => Good Work
        [image] => spotly
        [category] => 1
    )
)

我的代码

function name($var)
{
    return (is_array($var) && $var['Name'] == preg_match("/^Good/", $var['Name']));
}

print_r(array_filter($var, "name"));

谢谢。

4

2 回答 2

2

我会说只是做一个substr:

if ( substr( $var[ 'Name' ], 4 ) == 'Good' ) {
  // do what you want
}
于 2012-07-02T12:20:19.550 回答
1

删除 $var['Name']=

function name($var)
{
    return (is_array($var) && preg_match("/^Good/", $var['Name']));
}
于 2012-07-02T12:15:31.647 回答