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 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘