doujiayuan8415 2013-05-05 02:56
浏览 33
已采纳

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;

{"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.

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 // <<.

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).

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.

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.

So, any ideas what I'm doing wrong with the function? I'm sure it's something incredibly simple that I'm overlooking.

  • 写回答

1条回答 默认 最新

  • doubing3662 2013-05-05 03:03
    关注

    Your file is not JSON for an erray. The correct JSON for an array is

    [
     {"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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 这个电路是如何实现路灯控制器的,原理是什么,怎么求解灯亮起后熄灭的时间如图?
  • ¥15 matlab数字图像处理频率域滤波
  • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?
  • ¥15 ubuntu系统下挂载磁盘上执行./提示权限不够
  • ¥15 Arcgis相交分析无法绘制一个或多个图形
  • ¥15 关于#r语言#的问题:差异分析前数据准备,报错Error in data[, sampleName1] : subscript out of bounds请问怎么解决呀以下是全部代码:
  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)