You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何从游戏物品JSON中通过名称查询对应ID(含大小写不敏感处理)

Solution for RS3 Item Name to ID Lookup

Hey there! Let's walk through how to build this item name-to-ID lookup, including handling case insensitivity and connecting your input to the JSON data.

Step 1: Fetch and Parse the JSON Data

First, we need to grab the JSON file and convert it into a PHP array we can work with. Using json_decode with true as the second parameter gives us an associative array, which makes key-value lookups straightforward.

// Fetch the item JSON data
$jsonData = file_get_contents('http://mooshe.pw/files/items_rs3.json');

// Decode into an associative array (so we can use item names as keys)
$items = json_decode($jsonData, true);

// Basic error checking in case the JSON fails to load/parse
if ($items === null) {
    die('Oops, failed to load or parse the item data.');
}

Step 2: Handle Input and Case Insensitivity

By default, PHP array key matches are case-sensitive—so "Dwarf Remains" and "dwarf remains" would be treated as different keys. To fix this, we'll normalize both the input name and the JSON keys to lowercase (or uppercase, your choice) before comparing.

First, clean up the input from the $_GET parameter:

// Get the input name, with a fallback if the parameter is missing
$itemName = $_GET['name'] ?? '';

// Trim whitespace and convert to lowercase for case-insensitive search
$searchName = strtolower(trim($itemName));

// Make sure we have a valid input
if (empty($searchName)) {
    die('Please provide an item name using the "name" query parameter (e.g., ?name=dwarf remains).');
}

Next, create a case-insensitive lookup array. This is more efficient than looping through all items every time you search, especially if the JSON has a lot of entries:

// Build a lookup array where all keys are lowercase
$caseInsensitiveLookup = [];
foreach ($items as $itemNameFromJson => $itemId) {
    $caseInsensitiveLookup[strtolower($itemNameFromJson)] = $itemId;
}

Step 3: Perform the Lookup

Now we can check if our normalized search name exists in the lookup array and return the corresponding ID:

// Check if the item exists in our case-insensitive lookup
if (isset($caseInsensitiveLookup[$searchName])) {
    // Return the ID (e.g., 0 for "dwarf remains")
    echo $caseInsensitiveLookup[$searchName];
} else {
    echo "Sorry, no item found with that name.";
}

Full Working Code

Putting it all together, here's the complete script:

<?php
// Get and clean input
$itemName = $_GET['name'] ?? '';
$searchName = strtolower(trim($itemName));

if (empty($searchName)) {
    die('Please provide an item name via the "name" parameter (e.g., ?name=dwarf remains).');
}

// Load and parse JSON data
$jsonData = file_get_contents('http://mooshe.pw/files/items_rs3.json');
$items = json_decode($jsonData, true);

if ($items === null) {
    die('Error loading item data.');
}

// Build case-insensitive lookup
$caseInsensitiveLookup = [];
foreach ($items as $name => $id) {
    $caseInsensitiveLookup[strtolower($name)] = $id;
}

// Execute lookup
if (isset($caseInsensitiveLookup[$searchName])) {
    echo $caseInsensitiveLookup[$searchName];
} else {
    echo "Item not found.";
}
?>

Quick Note on Case Sensitivity

  • Default behavior (case-sensitive): If you wanted to keep matches case-sensitive, you could skip the lowercase conversion and just check isset($items[$itemName]) directly.
  • Case-insensitive (as above): By normalizing both input and JSON keys to the same case, we ensure that "Dwarf Remains", "DWARF REMAINS", and "dwarf remains" all return the correct ID.

内容的提问来源于stack exchange,提问作者Henry D.

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 07:48:32