0

I'm making a GET request to shopify API, and it gives back a response in json. I'm then using pHp to take the response and transform it into arrays put into a csv file. But I'm having a error.

The php code:

<?php
$baseUrl = 'https://[email protected]/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);
?>
This is giving me an error:

 Warning: fputcsv() expects parameter 2 to be array, object given in C:\wamp\www\test\index.php on line 57 

where line 57 is : fputcsv($fp, $item);

Can you please help me how to make it an array and insert into csv?

@Squeegy, thanks for your answer. It is working so far, the only problem is that in the csv its missing some important values. In the echo they show great and here its a part of the first loop:

 

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)
 

But in the csv None of the values I wrote above is included. Any idea why?

7
  • Is this what it looks like when you do (array) $item? If that's the case you'll need to go one level deeper in the loop still. Because you have an array now, but the value of each key is still an object. And unless that object has an __toString() function, it'll just result in NULL. So that may be why your CSV is still empty. Commented May 12, 2015 at 14:54
  • The csv is not empty, but doesnt have the echo values Commented May 12, 2015 at 14:56
  • Its in the csv file, something like: manual, 1046740548, 455951420, 1, 1, 1, Athletic Style Auto renew (ships every 1 Months), 1292559264, Athletic Style Auto renew (ships every 1 Months), Array, 1, 1, 0, Array Commented May 12, 2015 at 15:03
  • The shopify API after the GET request gives back a long response in json with many values, but after I dump most of that in the end I get the echo with the first loop that I wrote in the question. I just want the echo response to be in the csv. var_dump($item->properties); //this is the part that dumps all unnecessary data – Commented May 12, 2015 at 15:05
  • Yup, that sounds about right. fputcsv() doesn't work recursively. So any array value that's not a string, it just casts to a string (string) $value and puts that as the result. You'll need to do some further work organizing the structure into something you want, because ultimately each time you call fputcsv() you need to be passing it a simple array like array('1234', 'foo', 'bar') and nothing else. And without knowing the exact structure of your JSON, I can't help you much beyond just suggesting to step back and plan out what the end result you're looking for is. Commented May 12, 2015 at 15:09

1 Answer 1

1
$array = (array) $item;

fputcsv($fp, $array);

Found here Convert PHP object to associative array

Sign up to request clarification or add additional context in comments.

6 Comments

or instead of type cast why not use the true flag in the json_decode instead
@Ghost then hi should rewrite the loops
@Ghost good point. But then he would have to rewrite the loops. My answer was pretty quick and dirty.
@Squeegy yeah should have used that before all of those but if it works then should be fine
@Squeegy, please give a look to the updated question. Its to long to write in the comments section
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.