简体   繁体   中英

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;

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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