首页 > 解决方案 > 无法删除括号中的值并更新值

问题描述

我通过 API 从 CS:GO 游戏中获取项目列表。项目名称例如如下:AUG | Syd Mead (Well-Worn),在解析时我尝试删除 this: (Well-Worn),我有代码,但它不会更新信息,也不会删除带括号的值。

API 结果示例:

{
    "success": true,
    "currency": "USD",
    "timestamp": 1608675023,
    "items_list": {
                 },
        "1st Lieutenant Farlow | SWAT": {
            "name": "1st Lieutenant Farlow | SWAT",
            "rarity": "Superior",
            "rarity_color": "d32ce6",
            "price": {
                "24_hours": {
                    "average": 2.45,
                    "median": 2.45,
                    "sold": "444",
                    "standard_deviation": "3.41",
                    "lowest_price": 2.37,
                    "highest_price": 2.63
                },
             }
        },
        "AK-47 | Aquamarine Revenge (Battle-Scarred)": {
            "name": "AK-47 | Aquamarine Revenge (Battle-Scarred)",
            "exterior": "Battle-Scarred",
            "rarity": "Covert",
            "rarity_color": "eb4b4b",
            "price": {
                "7_days": {
                    "average": 15.48,
                    "median": 15.39,
                    "sold": "299",
                    "standard_deviation": "5.17",
                    "lowest_price": 11.2,
                    "highest_price": 17.36
                },
            },
        }

你怎么看,第一项没有exterior,第二项有exterior响应。

我的功能是更新我需要的所有信息:

public function update_information()
{
    $prices = json_decode(file_get_contents('http://csgobackpack.net/api/GetItemsList/v2/?prettyprint=true'), true);

    if (!$prices['success']) {
        dd('Error');
    }

    $newPrices = [];
    $newColor = [];

    foreach ($prices['items_list'] as $price) {
        $newPrices[$price['name']] = $price['rarity'];
        $newColor[$price['name']] = $price['rarity_color'];
    }

    $totalUpdated = 0;

    foreach (AllItem::query()->get() as $itemDB) {
        $fullName = $itemDB->market_hash_name;
        $newName = trim(str_replace('('.$itemDB->exterior.')', '', $fullName));

        if (!isset($newPrices[$fullName])) {
            continue;
        }

        if (!isset($newColor[$fullName])) {
            continue;
        }

        if (!isset($newName)) {
            continue;
        }

        if (!isset($itemDB->exterior)) {
            continue;
        }

        $itemDB->update(['market_hash_name' => $newName]);
        $itemDB->update(['exterior' => $newPrices[$fullName]]);
        $itemDB->update(['rarity' => $newPrices[$fullName]]);
        $itemDB->update(['rarity_color' => $newColor[$fullName]]);
        $totalUpdated++;
    }

    dd('done. Updated images: ' . $totalUpdated);
}

foreach我有$newName需要 makestr_replace和 function的变量,需要->update更新数据库,但它不起作用。

我也尝试更改为:

 if(!empty($itemDB->exterion)) {
        $newName = str_replace('('.$itemDB->exterior.')', '', $fullName);
 }

empty($itemDB->exterion)因为有些项目没有外部(例如我们可以看到它)。但我在我的表中收到了 0 个更新:(

我的错误在哪里?也许$newName变量是错误的?如何删除括号并在数据库中进行更新?

标签: phplaravel

解决方案


推荐阅读