PHP 中的编辑

发布于 2024-11-04 21:14:55 字数 1441 浏览 3 评论 0原文

我已经在模型中编写了这些函数(我正在使用 CodeIgniter)。

    function getLocalIngrdname()
{
    $this->load->database();
    $query = $this->db->get('onl_local_ingrd');
    foreach($query->result() as $row)
        $allingrd[]=$row->ingrd_localname;
    return $allingrd;
}

    function getCloseIngrdname($ingrdname,$localname)
{
    $this->load->database();
    $query = $this->db->get('onl_ingrd');
    foreach($query->result() as $row)
        $allingrd[]=$row->ingrd_name;

    foreach($localname as $row)
        $allingrd[]=$row;

            $shortest=-1;
            foreach ($allingrd as $ingrd) {
        $lev = levenshtein($ingrdname, $ingrd);
        if ($lev <= $shortest || $shortest < 0) {
            $closeword  = $ingrd;
            $shortest = $lev;

        }
    }
        return $closeword;
   }

该函数位于控制器中

   function getResult()
   {
    $this->load->model('searchRecipe_model');
    $ingrdname = $this->input->post('ingrdname');
    $output[]=2;
    $localnames[]=$this->searchRecipe_model->getLocalIngrdname();
    $output[]=$this->searchRecipe_model->getCloseIngrdname($ingrdname,$localnames);
    echo json_encode($localnames); 
  }

$allingrd 是一个成分名称数组。我在 javascript 警报消息中显示 $closeword 。 如果我给出$lev=levenshtein($ingrdname,$allingrd[0]),它就可以完美工作;但是,它不在循环中工作。有什么想法吗?提前致谢。

I have written these function in a model (I am using CodeIgniter).

    function getLocalIngrdname()
{
    $this->load->database();
    $query = $this->db->get('onl_local_ingrd');
    foreach($query->result() as $row)
        $allingrd[]=$row->ingrd_localname;
    return $allingrd;
}

    function getCloseIngrdname($ingrdname,$localname)
{
    $this->load->database();
    $query = $this->db->get('onl_ingrd');
    foreach($query->result() as $row)
        $allingrd[]=$row->ingrd_name;

    foreach($localname as $row)
        $allingrd[]=$row;

            $shortest=-1;
            foreach ($allingrd as $ingrd) {
        $lev = levenshtein($ingrdname, $ingrd);
        if ($lev <= $shortest || $shortest < 0) {
            $closeword  = $ingrd;
            $shortest = $lev;

        }
    }
        return $closeword;
   }

This function is in controller

   function getResult()
   {
    $this->load->model('searchRecipe_model');
    $ingrdname = $this->input->post('ingrdname');
    $output[]=2;
    $localnames[]=$this->searchRecipe_model->getLocalIngrdname();
    $output[]=$this->searchRecipe_model->getCloseIngrdname($ingrdname,$localnames);
    echo json_encode($localnames); 
  }

$allingrd is an array of ingredient names. I display the $closeword in javascript alert message.
If I give$lev=levenshtein($ingrdname,$allingrd[0]) it works perfectly; however, it's not working in the loop. Any ideas as to why? Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

迷爱 2024-11-11 21:14:56

也许您有一些长度超过 255 个字符的字符串?在这些情况下,levenshtein() 函数返回 -1,这意味着根据您的算法,最后一个此类字符串将始终是“最短的”。如果这是您的问题,您需要以这种方式丢弃 $lev 等于 -1 的值:

foreach ($allingrd as $ingrd) {
        $lev = levenshtein($ingrdname, $ingrd);
        if (($lev <= $shortest || $shortest < 0) && $lev != -1) {
            $closeword  = $ingrd;
            $shortest = $lev;

        }
}
return $closeword;

请注意,$closeword 可能并不总是被设置(例如,如果 $allingrd 中的所有字符串都超过 255 个字符,或者如果 $allingrd 为空)。不要忘记在循环之前将 $closeword 初始化为合理的值,例如 false、null 或空字符串。

Maybe you have som strings which are longer than 255 chars? The levenshtein() function returns -1 in those cases, which means the last such string will always be "the shortest" according to your algorithm. If this is your problem, you'll need to discard values where $lev equals -1 in this way:

foreach ($allingrd as $ingrd) {
        $lev = levenshtein($ingrdname, $ingrd);
        if (($lev <= $shortest || $shortest < 0) && $lev != -1) {
            $closeword  = $ingrd;
            $shortest = $lev;

        }
}
return $closeword;

Note that $closeword may not always be set (if, for example, all strings in $allingrd is longer than 255 chars, or if $allingrd is empty). Don't forget to initialise $closeword to something sensible before the loop, for example false, null or an empty string.

热鲨 2024-11-11 21:14:56

我认为你想要做的是:

foreach ($allingrd as $ingrd) {
        $lev = levenshtein($ingrdname, $ingrd);
        if ($lev <= $shortest || $shortest < 0) {
            $closeword[] = $ingrd;
            $shortest[] = $lev;

        }
}
return $closeword;

现在 $closeword 是一个数组。您只是在每次循环运行时重新声明 $closeword 的值。现在,您将把值放入数组中。最短的美元也一样。

I think that what you're trying to do is:

foreach ($allingrd as $ingrd) {
        $lev = levenshtein($ingrdname, $ingrd);
        if ($lev <= $shortest || $shortest < 0) {
            $closeword[] = $ingrd;
            $shortest[] = $lev;

        }
}
return $closeword;

Now $closeword is an array. You were just redeclaring the value of $closeword, for each time your loop was running. Now, you will throw the values inside an array. Same for $shortest.

涙—继续流 2024-11-11 21:14:56

请注意 PHP 的 levenshtein 页面上如何声明 $shortest-1。这非常重要,否则您将永远无法满足 if 语句的要求。

$shortest = -1;
foreach ($allingrd as $ingrd) {
        $lev = levenshtein($ingrdname, $ingrd);
        if ($lev <= $shortest || $shortest < 0) {
            $closeword  = $ingrd;
            $shortest = $lev;

        }
}
return $closeword;

这应该可以解决问题。

Notice how on PHP's levenshtein page they declare $shortest as -1. This is pretty important or else you'll never meet the requirements of the if statement.

$shortest = -1;
foreach ($allingrd as $ingrd) {
        $lev = levenshtein($ingrdname, $ingrd);
        if ($lev <= $shortest || $shortest < 0) {
            $closeword  = $ingrd;
            $shortest = $lev;

        }
}
return $closeword;

This should do the trick.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文