【问题标题】:Match Similarity inside array in PHP匹配PHP中数组内的相似度
【发布时间】:2021-03-04 08:46:56
【问题描述】:

我有一个这样的 json 文件:

{
  "results": [
    {
      "category_id": 13732,
      "name": "Artificial Fruits & Vegetables",
      "category_tree": "Home, Garden & Pets > Home Furnishings & Decor > Artificial Plants & Flowers",
      "google_category": {
        "id": 6265,
        "tree": "Home & Garden > Decor > Artificial Flora",
        "name": "Artificial Flora",
        "level": 3
      }
    },
    {
      "category_id": 13981,
      "name": "Vegetables",
      "category_tree": "Home, Garden & Pets > Garden & Outdoor Living > Plants, Seeds & Bulbs > Outdoor Plants",
      "google_category": {
        "id": 6762,
        "tree": "Home & Garden > Plants > Indoor & Outdoor Plants",
        "name": "Indoor & Outdoor Plants",
        "level": 3
      }
    },
    {
      "category_id": 17779,
      "name": "Vegetables",
      "category_tree": "Food & Drink > Food Cupboard > Tins, Cans & Jars",
      "google_category": {
        "id": 5798,
        "tree": "Food, Beverages & Tobacco > Food Items > Fruit & Vegetables > Canned & Jarred Vegetables",
        "name": "Canned & Jarred Vegetables",
        "level": 4
      }
    },
    {
      "category_id": 17800,
      "name": "Vegetables",
      "category_tree": "Food & Drink > Frozen Food",
      "google_category": {
        "id": 5793,
        "tree": "Food, Beverages & Tobacco > Food Items > Fruit & Vegetables > Fresh & Frozen Vegetables",
        "name": "Fresh & Frozen Vegetables",
        "level": 4
      }
    },
    {
      "category_id": 17806,
      "name": "Vegetables",
      "category_tree": "Food & Drink > Fruit & Veg",
      "google_category": {
        "id": 430,
        "tree": "Food, Beverages & Tobacco > Food Items > Fruit & Vegetables",
        "name": "Fruit & Vegetables",
        "level": 3
      }
    }
  ]
  }

我想获取与此字符串最相关/最相似的类别

“Ocado 自有标签/水果、蔬菜和沙拉/蔬菜”

通过类别树或 json 中每个类别对象的名称进行匹配。

请帮忙,我是 PHP 新手

...更新...

<?php
similar_text("Beverages & Tobacco > Food Items > Fruit & Vegetables > Fresh & Frozen Vegetables","Ocado Own-Label/Fruit, Vegetables & Salad/Vegetables",$percent);
echo $percent;
?>

我想使用similar_text函数搜索并返回最高匹配的json对象/类别

【问题讨论】:

  • 向我们展示你的尝试,你最好的尝试(代码)。什么不能按预期工作?你有任何错误吗?推荐阅读How to Ask.
  • 问题已更新
  • 那么,在循环中执行此操作,如果当前项目的百分比高于您之前的最高百分比,那么当前项目将成为您的新“最佳候选”。
  • 第一场比赛呢?如果什么都没有,我将如何与之前的比赛进行比较
  • 为此,您只需要在逻辑中为上一个匹配项为空时设置一个特殊情况 - 在这种情况下,当前项目总是成为“顶部”项目(显然)。

标签: php json match


【解决方案1】:
$decode = json_decode($json);
for($i=0; $i<count($decode->results); $i++){
    
    similar_text($decode->results[$i]->google_category->tree,"Ocado Own-Label/Fruit, Vegetables & Salad/Vegetables",$percent);
    $myarr[$i]=$percent;
}

$maxmatchcategory = array_search(max($myarr), $myarr);
echo $decode->results[$maxmatchcategory]->category_id.'<br>';

【讨论】:

    猜你喜欢
    • 2022-08-18
    • 2015-05-05
    • 1970-01-01
    • 1970-01-01
    • 2021-08-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多