在 php 上格式化和拆分 csv/json



我正在尝试使用此API来填充我的mysql数据库,如果出现"新itens",我需要更新,并更新所有价格。我已经有了这个脚本的后端结构,但我需要将名称与每个项目的价格分开。

API(您可以在浏览器上打开(:https://api.csgofast.com/price/all

我怎样才能做到这一点,比如,最好的方法?

我试图在":"上爆炸,但情况如下:

"Prismatic: Miasmatic Grey": 39.48,

所以爆炸不起作用。

谢谢建议。

代码:

protected function populateItems()
{
$items = json_decode(file_get_contents(API_URL));
foreach($items as $item) {
Item::create([
'name' => $i[0],
'price' => $i[1]
]);
}
die('end');
}

我也试过这个,但它在":"上失败了

protected function populateItems()
{
$body = file_get_contents(API_URL);
$items_parsed = str_replace('{', '', $body);
$items_converted = str_replace('}', '', $items_parsed);
$items = str_getcsv($items_converted);
foreach($items as $item) {
// Item::create([
// 'name' => $i[0],
// 'price' => $i[1]
// ]);
}
echo 'Fim da execução.';
die;
}

回答:

object(stdClass)#470 (15689) { ["Sticker | Counter Logic Gaming (Foil) | MLG 
Columbus 2016"]=> float(2.14) ["Sticker | RUBINO (Foil) | Cologne 2016"]=> 
float(1.26) ["P2000 | Granite Marbleized (Well-Worn)"]=> float(0.01) ["★ Bayonet 
| Fade (Minimal Wear)"]=> float(226.55)

我刚刚遇到了问题。我不习惯使用 a for each 和 $key => $value,所以我这样解决:

$items = json_decode(file_get_contents(API_URL));
foreach($items as $item => $price) {
Item::create([
'name' => $item,
'price' => $price
]);
}