简体   繁体   English

in_array的dataype错误

[英]Wrong dataype for in_array

Good morning. 早上好。

I'm currently trying to build a very basic caching system for one of my scripts. 我正在尝试为我的一个脚本构建一个非常基本的缓存系统。 The cache is JSON data and contains only 1 key and it's value, but many individual fields, something like this; 缓存是JSON数据,只包含1个密钥及其值,但是包含许多单独的字段,如下所示;

{"Item1":"Item1 Description"}
{"Item2":"Item2 Description"}
{"Item3":"Item3 Description"}

What I'm intending to do is; 我打算做的是;

  • First check if a cache file is available 首先检查缓存文件是否可用
  • Then check if an item exists in the cache 然后检查缓存中是否存在某个项目
  • Then add the new item along with it's description if it's not already in the cache... 然后添加新项目及其描述,如果它尚未在缓存中...
  • ...or return the item description if it's not there. ...如果不存在,则返回项目描述。

All data being stored is strings. 存储的所有数据都是字符串。 The cache file doesn't store any other type of data. 缓存文件不存储任何其他类型的数据。

I've put together a basic function but I'm having trouble getting it functioning; 我已经整理了一个基本的功能,但是我无法让它运转起来;

function ItemIsInCache($CacheFile, $ItemId) {
  if(file_exists($CacheFile)) {
    $json = json_decode(file_get_contents($CacheFile, true));
    if(in_array($ItemId, $json)) { // <<
      $itemname = array_search($ItemId, $json);
      return itemname;
    } else {
      $item[$itemId] = GrabItemName($ItemId);
      $itemname = array_search($ItemId, $json); // <<
      return $itemname;
    }
  } else {
    $item[$ItemId] = GrabItemName($ItemId);
    $ejson = json_encode($item);
    file_put_contents($CacheFile, $ejson);
    return $item[$ItemId];    
  }
}

Notes 笔记

GrabItemName is a different function that returns the description data based on the $ItemId . GrabItemName是一个不同的函数,它根据$ItemId返回描述数据。

The warnings I'm getting are Wrong datatype for second argument in both array_search() and in_array() , on lines 4 and lines 9 respectively (those are the line numbers in the above code - due to the nature of my script these numbers are later on) -- for simplicity, I've marked the problem lines with // << . 我得到的警告是array_search()in_array() Wrong datatype for second argument分别在第4行和第9行(这些是上面代码中的行号 - 由于我的脚本的性质,这些数字是稍后) - 为简单起见,我用// <<标记问题行。

The function is running in a loop which I've no problems with. 该函数在循环中运行,我没有遇到任何问题。 The problems lie within this function. 问题在于这个功能。

What currently happens 目前发生了什么

Right now, if the cache doesn't exist, it creates it and adds the first item from the loop to the cache file in it's respective JSON format (that fires since the cache file doesn't exist, so after the final else statement). 现在,如果缓存不存在,它会创建它并将循环中的第一个项添加到缓存文件中,其中包含相应的JSON格式(由于缓存文件不存在而触发,因此在最后的else语句之后) 。

However, items from the loop after that don't get added, presumably because the file exists and there's something wrong with the code. 但是,之后循环中的项目不会被添加,大概是因为文件存在且代码有问题。

The last part of the function works exactly as I want it to but the first part does not. 函数的最后一部分完全按照我的要求工作,但第一部分却没有。

Expected behaviour with fixed code 使用固定代码的预期行为

Check cache > Return description if item exists ELSE add new item to cache. 如果项目存在,则检查缓存>返回描述ELSE将新项目添加到缓存。

The items and their associated descriptions will NOT change, but I'm pulling them from a rate limited API, and I need to ensure I cache whatever I can for everyones benefit. 这些项目及其相关描述不会改变,但我会从速率有限的API中提取它们,我需要确保尽可能地为每个人的利益缓存。

So, any ideas what I'm doing wrong with the function? 那么,任何想法我在做什么错误的功能? I'm sure it's something incredibly simple that I'm overlooking. 我敢肯定这是一件令人难以置信的简单事情。

Your file is not JSON for an erray. 对于erray,您的文件不是JSON。 The correct JSON for an array is 数组的正确JSON是

[
 {"Item1":"Item1 Description"},
 {"Item2":"Item2 Description"},
 {"Item3":"Item3 Description"}
]

You're missing the brackets around the array, so you just get a single object. 你错过了数组周围的括号,所以你只得到一个对象。

When creating the initial file, you need to do: 创建初始文件时,您需要执行以下操作:

    $ejson = json_encode(array($item));

so that it's initialized as an array of one item, not just an item. 这样它就被初始化为一个项目的数组,而不仅仅是一个项目。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM