php - 无法删除括号中的值并更新值
问题描述
我通过 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
变量是错误的?如何删除括号并在数据库中进行更新?
解决方案
推荐阅读
- php - 如何获取 Woocommerce 产品的所有类别
- javascript - How do I setup a "rolling window"?
- python-3.x - 为什么输入刷新标准输出
- vue.js - 每页的行数未正确定位
- imagemagick - 如何使用 libwebp 编译 Imagick 以获得 Webp 图像支持
- r - R Compare rows in the same dataset by ID to find out if one comes before one
- git - 更新分支不是当前的 master
- react-native - 无法更新 MapView 的 onRegionChange() 方法中的状态变量
- python - 平均每四个二维numpy数组python
- search - TFS 升级到 Azure DevOps - 是否可以知道搜索选项的用户是什么