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/8/mysql/64.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将CSV条目与JSON文件进行比较_Php_Mysql_Json_Csv - Fatal编程技术网

使用PHP将CSV条目与JSON文件进行比较

使用PHP将CSV条目与JSON文件进行比较,php,mysql,json,csv,Php,Mysql,Json,Csv,我有一个名为冰箱.CSV的CSV文件,如下所示: +---------------+---------+--------+------------+ | item | amount | unit | use by | +---------------+---------+--------+------------+ | bread | 10 | slices | 25/12/2017 | +---------------+--------

我有一个名为
冰箱.CSV
的CSV文件,如下所示:

+---------------+---------+--------+------------+
| item          |  amount |  unit  |  use by    |
+---------------+---------+--------+------------+
| bread         | 10      | slices | 25/12/2017 |
+---------------+---------+--------+------------+
| cheese        | 10      | slices | 25/12/2017 |
+---------------+---------+--------+------------+
| butter        | 250     | grams  | 25/12/2017 |
+---------------+---------+--------+------------+
| peanut butter | 250     | grams  | 2/12/2017  |
+---------------+---------+--------+------------+
| mixed salad   | 500     | grams  | 26/12/2016 |
+---------------+---------+--------+------------+
[
  {
    "name": "grilledcheeseontoast",
    "ingredients": [
      {
        "item": "bread",
        "amount": "2",
        "unit": "slices"
      },
      {
        "item": "cheese",
        "amount": "2",
        "unit": "slices"
      }
    ]
  },
  {
    "name": "saladsandwich",
    "ingredients": [
      {
        "item": "bread",
        "amount": "2",
        "unit": "slices"
      },
      {
        "item": "mixedsalad",
        "amount": "200",
        "unit": "grams"
      }
    ]
  },
  {
    "name": "peanutbuttersandwich",
    "ingredients": [
      {
        "item": "bread",
        "amount": "2",
        "unit": "slices"
      },
      {
        "item": "peanut butter",
        "amount": "250",
        "unit": "grams"
      }
    ]
  }
]
还有一个名为
recipes.JSON
的JSON文件,如下所示:

+---------------+---------+--------+------------+
| item          |  amount |  unit  |  use by    |
+---------------+---------+--------+------------+
| bread         | 10      | slices | 25/12/2017 |
+---------------+---------+--------+------------+
| cheese        | 10      | slices | 25/12/2017 |
+---------------+---------+--------+------------+
| butter        | 250     | grams  | 25/12/2017 |
+---------------+---------+--------+------------+
| peanut butter | 250     | grams  | 2/12/2017  |
+---------------+---------+--------+------------+
| mixed salad   | 500     | grams  | 26/12/2016 |
+---------------+---------+--------+------------+
[
  {
    "name": "grilledcheeseontoast",
    "ingredients": [
      {
        "item": "bread",
        "amount": "2",
        "unit": "slices"
      },
      {
        "item": "cheese",
        "amount": "2",
        "unit": "slices"
      }
    ]
  },
  {
    "name": "saladsandwich",
    "ingredients": [
      {
        "item": "bread",
        "amount": "2",
        "unit": "slices"
      },
      {
        "item": "mixedsalad",
        "amount": "200",
        "unit": "grams"
      }
    ]
  },
  {
    "name": "peanutbuttersandwich",
    "ingredients": [
      {
        "item": "bread",
        "amount": "2",
        "unit": "slices"
      },
      {
        "item": "peanut butter",
        "amount": "250",
        "unit": "grams"
      }
    ]
  }
]

我的任务是使用php创建一个控制台应用程序,将这两个文件放在一行参数中,并根据冰箱中的项目输出推荐的食谱。不能使用过期的成分。我该怎么做呢?(我是PHP初学者)

首先从CSV文件中筛选出所有过期日期的内容(例如,当前日期比CSV文件中列出的日期大)。PHP内置了对读取和写入CSV文件的支持
str_getcsv()
就是其中之一。可以找到以下文档:。您可以使用此函数逐个读取文件并将其转换为数组。这些评论也提供了一些有用的指针

过滤后,使用内置的文件读取功能读取JSON文件,例如
file\u get\u contents()
()。这将把filename参数指向的给定文件读入字符串。使用内置的
json_decode()
()函数将输入字符串转换为对象或关联数组

将JSON文件转换为对象或数组后,可以循环过滤成分列表并检查配方数据中是否存在匹配项。您可以采用直接的方法,通过反复循环您的配方数据,直到找到匹配项,或者尝试使用和/或组合更多内置函数来解决问题


尝试将您的解决方案分解为更小的问题,并专注于解决这些问题。

您使用的是composer吗?我不知道composer是什么,我使用的是XAMPP环境,如果这正是您需要的。您好,感谢您的回复,根据您的建议,我开发了以下代码
https://justpaste.it/1b57n
。我很难创建循环来匹配从csv文件和json文件获得的数组,以及如何创建循环的任何说明。希望到目前为止,我正朝着正确的方向前进。