【问题标题】:Matching items in two arrays匹配两个数组中的项目
【发布时间】:2015-08-04 17:08:05
【问题描述】:

是的,我有两个数组,我正在尝试比较它们之间的单词是否匹配。

$address = explode(",", '31 Birmingham Road, Erdington, Birmingham, Blah');

我正在使用以下没有找到任何匹配项但是在探索循环时我可以得到 Erdington = Erdington 并且它仍然没有返回匹配项?

if (count($_POST['location'])) {
    foreach ($_POST['location'] as $value) {
        if (in_array($value, $address)) {
            $exists = 1;
            $success[] = "Match";
        }
    }
}

有什么想法吗?

【问题讨论】:

  • 逗号后面的空格!!!!!

标签: php arrays string-matching


【解决方案1】:

逗号后面的空格可能导致条件失败。

' Erdington' != 'Erdington'

脏测试(在逗号后的分隔符中添加一个空格)

$address = explode(", ", '31 Birmingham Road, Erdington, Birmingham, Blah');

【讨论】:

    【解决方案2】:

    逗号后面的空格导致测试失败。如果地址总是有那个空间,你可以使用@SharpEdge 的答案。如果是可选的,请使用trim()

    $address = array_map('trim', explode(",", '31 Birmingham Road, Erdington, Birmingham, Blah'));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-06
      • 1970-01-01
      • 1970-01-01
      • 2022-11-18
      • 1970-01-01
      • 2021-08-05
      相关资源
      最近更新 更多