laravel In_array() 字符串检测的函数部分

laravel In_array() function part of string detect

数组如下:

$array = ['blog.setting', 'blog.post', 'blog.delete', 'blog.edit', 'other.other'];

我想知道是否有元素 'blog.'; 也许这样就完美了。

in_array('blog.*', $array)

谁能帮帮我?

你需要使用正则表达式,array_filter方法

$array = ['blog.setting', 'blog.post', 'blog.delete', 'blog.edit', 'other.other'];

$filtered = array_filter($array, function($value){
    return preg_match('/^blog\..*$/m', $value);
});

正如您在标签中提到的 Laravel,您可以:

use Illuminate\Support\Str;
use Illuminate\Support\Collection;

$blogTest = Collection::make($array)
    ->contains(function ($elt) {
        return Str::is('blog.*', $elt);
    });

你可以用纯 PHP 来做:

    $array = ['blog.setting', 'blog.post', 'blog.delete', 'blog.edit', 'other.other'];
    $string = implode(',',$array);

    if (strpos($string, 'blog') !== false) {
        dump('There is blog string inside of given array!');
        return true;
    }