如何在数组中找到条件词

How to make if condition word found in array

在PHP

echo $avatar["name"];
$array = ["hello", "world", "!"];
$string = "wor";
if (in_array($string, [$array])){
    echo "match";
}else
    echo "not match";

我想要一个这样的脚本来打印匹配数组中的单词,而不是等于数组的字符串

你可以这样试试:

function partial_array_search( $array, $keyword ) {
    $found = [];
    // Loop through each item and check for a match.
    foreach ( $array as $string ) {
        // If found somewhere inside the string, add.
        if ( strpos( $string, $keyword ) !== false ) {
            $found[] = $string;
        }
    }
    return $found;
}

$array = ["hello", "world", "!"];
$string = "wor";

$data = partial_array_search( $array, $string );
if($data){
    print_r($data) ;   
}else{
    echo 'NOT MATCH';
}