Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/269.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/flutter/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
比较php数组中的字符串元素_Php_Arrays - Fatal编程技术网

比较php数组中的字符串元素

比较php数组中的字符串元素,php,arrays,Php,Arrays,我将用一个简单的例子来解释我的问题。我有一个数组 $array1 = array( "Sun" => "1", "Lake Red" => "1", "Tree" => "1" ) 我有一句话要说 $word = "Lake"; 我想检查这些单词是否在此数组中。我是这样做的 if (isset($array1[$word])) { print "This word is in the array.\n\n"; } 这个代码,我已经没有做好我的工作,因为对于程序字

我将用一个简单的例子来解释我的问题。我有一个数组

$array1 =  array( "Sun" => "1", "Lake Red" => "1", "Tree" => "1" )
我有一句话要说

$word = "Lake";
我想检查这些单词是否在此数组中。我是这样做的

if (isset($array1[$word])) {
    print "This word is in the array.\n\n";
}
这个代码,我已经没有做好我的工作,因为对于程序字湖是不一样的湖红色。。。我如何解决这些问题,因此,如果其中一个元素的单词中至少有一个单词,那么应该写以下内容:

print "This word is in the array.\n\n";
  • 使用函数获取数组中的各种值,并将其附加到一个由空格(
    “”
    )分隔的字符串中
  • 现在,只需使用函数在上一步获得的组合字符串中检查
    $word
    子字符串
请尝试以下操作:

// Get all array elements concatenated as a string
$array1 = array("Lake Red", "Sun", "Tree");
$array1_str = implode(" ", $array1);

// use strpos to check if the $word exists
$word = "Lake";
if (strpos($array1_str, $word) !== false) {
    print "This word is in the array.\n\n";
}

您好,我建议您使用类似的方法,通过foreach循环数组,并将每个值与$word进行比较

$array1 = array(Lake Red, "Sun", "Tree");
$word = "Lake"
foreach($array1 as $w){
if($w==$word){
 print "This word is in the array.\n\n";
}

您可以在_数组中使用
来检查与搜索相同的值。例如

$array1 = array("Lake Red", "Sun", "Tree"); //note the " around Lake Red
$word = "Lake";`

if (in_array($word,$array1)) 
{
    print "This word is in the array.\n\n";
}
通过执行
检查(isset($array1[$word])
是否需要您实现如下数组,它将查找与您的搜索相同的

$array = array(
   "Lake" => "Red",
   "Lake Red" => "1",
   "Boo" => "Foo"
);
或者如果要在关联数组的键中搜索单词

$keys = array_keys($array);
for($i=0,$count = count($keys);$i<$count;$i++) {
    if (strpos($word, $keys[$i]) !== false) {
        print "This word is in the array.\n\n";
        break;
    }
}
但是,使用for循环进行此操作会更快、更优化。使用
内爆
会使大型数组出现内存问题。但如果我们要这么做的话,下面是方法

$keys = array_keys($array);
$long_str = implode(" ",$keys);
if (strpos($word, $long_str) !== false) {
    print "This word is in the array.\n\n";
    break;
}

我认为使用内爆是最好的解决方案,以前有人回答过。无论如何,我做了这个功能,它也可以工作:

$array = array("Lake Red", "Sun", "Tree");

function in_array_word($string, $array){
    foreach($array as $index){
        // Check if the current word we're looping is exact to what we look for.
        if(strcmp($index, $string) == 0){
            return true;
        }else if(strpos($index, " ") !== FALSE){
            // If is not the same, check if there are two or more words.
            $words = explode(" ", $index);
            foreach($words as $word){
                if(strcmp(trim($word), $string) == 0){
                    return true;
                }
            }
        }
    }
    return false;
}

var_dump(in_array_word("Lake Red", $array));
var_dump(in_array_word("Lake", $array));
var_dump(in_array_word("Red", $array));
var_dump(in_array_word("Moon", $array));
var_dump(in_array_word("Blue", $array));
var_dump(in_array_word("Tree", $array));

// If we have some spaces mistach will also work for this:
$array2 = array("Lake  Red", "Sun", "Tree");
var_dump(in_array_word("Lake", $array2));

// But not for:
var_dump(in_array_word("Lake Red", $array2));
如前所述,您需要获取字符串中数组的键,并在字符串中查找单词。我添加了较低的字符串并搜索了不区分大小写的单词:

$array1 =  array( "Sun" => "1", "Lake Red" => "1", "Tree" => "1" );
$array1_str = implode(" ", array_keys($array1));
$word = "Lake";
if (strpos(strtolower($array1_str), strtolower($word)) !== false) {
    print "This word is in the array.\n\n";
}

$array=array(“太阳”=>“1”、“红湖”=>“1”、“树”=>“1”);我的数组看起来像这样,这就是为什么我使用isset?我可以在数组中使用还是在isset中使用这种数组?对于第二种类型,你可以使用isset,但是如果你的数组像问题中的一样,它是第一种类型,你需要在数组中使用,我的数组看起来像第二种类型。我使用isset,但这对我来说不太好,因为单词Lake与Lake Redok不匹配,然后重新表述你的问题(根据你将要使用的第二种数组类型),你想在关联数组的键中搜索一个单词,对吗?
$array = array("Lake Red", "Sun", "Tree");

function in_array_word($string, $array){
    foreach($array as $index){
        // Check if the current word we're looping is exact to what we look for.
        if(strcmp($index, $string) == 0){
            return true;
        }else if(strpos($index, " ") !== FALSE){
            // If is not the same, check if there are two or more words.
            $words = explode(" ", $index);
            foreach($words as $word){
                if(strcmp(trim($word), $string) == 0){
                    return true;
                }
            }
        }
    }
    return false;
}

var_dump(in_array_word("Lake Red", $array));
var_dump(in_array_word("Lake", $array));
var_dump(in_array_word("Red", $array));
var_dump(in_array_word("Moon", $array));
var_dump(in_array_word("Blue", $array));
var_dump(in_array_word("Tree", $array));

// If we have some spaces mistach will also work for this:
$array2 = array("Lake  Red", "Sun", "Tree");
var_dump(in_array_word("Lake", $array2));

// But not for:
var_dump(in_array_word("Lake Red", $array2));
$array1 =  array( "Sun" => "1", "Lake Red" => "1", "Tree" => "1" );
$array1_str = implode(" ", array_keys($array1));
$word = "Lake";
if (strpos(strtolower($array1_str), strtolower($word)) !== false) {
    print "This word is in the array.\n\n";
}