json 到 csv php
json to csv with php
我正在向 shopify API 发出 GET 请求,它在 json 中返回响应。
然后我使用 pHp 获取响应并将其转换为放入 csv 文件的数组。但我有一个错误。
php代码:
<?php
$baseUrl = 'https://myapikey@mysite.myshopify.com/admin/';
$response = json_decode(file_get_contents($baseUrl.'orders.json?limit=250'));
$fp = fopen('file.csv', 'w');
var_dump($response->orders[0]->line_items[0]->properties);
foreach ($response->orders as $order_index => $order) {
foreach ($order->line_items as $item_index => $item) {
echo "order: {$order_index} item: {$item_index}";
var_dump($item->properties);
fputcsv($fp, $item);
}
}
fclose($fp);
?>
这给我一个错误:
Warning: fputcsv() expects parameter 2 to be array, object given in C:\wamp\www\test\index.php on line 57
第 57 行是:fputcsv($fp, $item);
你能帮我把它变成一个数组并插入到 csv 中吗?
@Squeegy,谢谢你的回答。到目前为止它正在工作,唯一的问题是在 csv 中它缺少一些重要的值。
在回声中,它们表现得很好,这是第一个循环的一部分:
order: 0 item: 0
array (size=9)
0 =>
object(stdClass)[6]
public 'name' => string 'Glove Size' (length=10)
public 'value' => string 'L' (length=1)
1 =>
object(stdClass)[7]
public 'name' => string 'Hat Size' (length=8)
public 'value' => string 'S/M' (length=3)
2 =>
object(stdClass)[8]
public 'name' => string 'Pant Size' (length=9)
public 'value' => string '32x34' (length=5)
3 =>
object(stdClass)[9]
public 'name' => string 'Right or Left Handed?' (length=21)
public 'value' => string 'Right' (length=5)
4 =>
object(stdClass)[10]
public 'name' => string 'Shirt Size' (length=10)
public 'value' => string 'S' (length=1)
5 =>
object(stdClass)[11]
public 'name' => string 'Shoe Size' (length=9)
public 'value' => string '10.5' (length=4)
6 =>
object(stdClass)[12]
public 'name' => string 'shipping_interval_frequency' (length=27)
public 'value' => string '1' (length=1)
7 =>
object(stdClass)[13]
public 'name' => string 'shipping_interval_unit_type' (length=27)
public 'value' => string 'Months' (length=6)
8 =>
object(stdClass)[14]
public 'name' => string 'subscription_id' (length=15)
public 'value' => string '1522' (length=4)
但是在 csv None 中包含了我上面写的值。知道为什么吗?
$array = (array) $item;
fputcsv($fp, $array);
在此处找到 Convert PHP object to associative array
我正在向 shopify API 发出 GET 请求,它在 json 中返回响应。 然后我使用 pHp 获取响应并将其转换为放入 csv 文件的数组。但我有一个错误。
php代码:
<?php
$baseUrl = 'https://myapikey@mysite.myshopify.com/admin/';
$response = json_decode(file_get_contents($baseUrl.'orders.json?limit=250'));
$fp = fopen('file.csv', 'w');
var_dump($response->orders[0]->line_items[0]->properties);
foreach ($response->orders as $order_index => $order) {
foreach ($order->line_items as $item_index => $item) {
echo "order: {$order_index} item: {$item_index}";
var_dump($item->properties);
fputcsv($fp, $item);
}
}
fclose($fp);
?>
Warning: fputcsv() expects parameter 2 to be array, object given in C:\wamp\www\test\index.php on line 57
第 57 行是:fputcsv($fp, $item);
你能帮我把它变成一个数组并插入到 csv 中吗?
@Squeegy,谢谢你的回答。到目前为止它正在工作,唯一的问题是在 csv 中它缺少一些重要的值。 在回声中,它们表现得很好,这是第一个循环的一部分:
order: 0 item: 0
array (size=9)
0 =>
object(stdClass)[6]
public 'name' => string 'Glove Size' (length=10)
public 'value' => string 'L' (length=1)
1 =>
object(stdClass)[7]
public 'name' => string 'Hat Size' (length=8)
public 'value' => string 'S/M' (length=3)
2 =>
object(stdClass)[8]
public 'name' => string 'Pant Size' (length=9)
public 'value' => string '32x34' (length=5)
3 =>
object(stdClass)[9]
public 'name' => string 'Right or Left Handed?' (length=21)
public 'value' => string 'Right' (length=5)
4 =>
object(stdClass)[10]
public 'name' => string 'Shirt Size' (length=10)
public 'value' => string 'S' (length=1)
5 =>
object(stdClass)[11]
public 'name' => string 'Shoe Size' (length=9)
public 'value' => string '10.5' (length=4)
6 =>
object(stdClass)[12]
public 'name' => string 'shipping_interval_frequency' (length=27)
public 'value' => string '1' (length=1)
7 =>
object(stdClass)[13]
public 'name' => string 'shipping_interval_unit_type' (length=27)
public 'value' => string 'Months' (length=6)
8 =>
object(stdClass)[14]
public 'name' => string 'subscription_id' (length=15)
public 'value' => string '1522' (length=4)
但是在 csv None 中包含了我上面写的值。知道为什么吗?
$array = (array) $item;
fputcsv($fp, $array);
在此处找到 Convert PHP object to associative array