Hello i'm working on a small project that i have to retrieve movies data from a json API, then save the result into a csv.
i'm able to display data with php eco but when i'm trying to write csv file i get the following
Warning: array_keys() expects parameter 1 to be array, object given on line 8
Warning: fputcsv() expects parameter 2 to be array, null given on line 9
Warning: array_flip() expects parameter 1 to be array, null given on line 10
Warning: array_merge(): Argument #1 is not an array on line 11
Warning: fputcsv() expects parameter 2 to be array, null given 11

$toplink = "https://api.themoviedb.org/3/movie/top_rated?api_key=3356bddf22b611f259916342a1a3ed50"
$toprated = file_get_contents($toplink);
   $toprated = json_decode($toprated);
   $top = $toprated->results;
    $header = false;
    foreach ($top as $row){
        if (empty($header)){
            $header = array_keys($row);
            fputcsv($fp, $header);
            $header = array_flip($header);
          }
        fputcsv($fp, array_merge($header, $row));
    }
    fclose($fp);
 ?>

What happens if you put var_dump($toprated); after the third line of the code you provided above. This way, for debugging purposes, we can see exactly what the $toprated object looks like.

However, I think I see your problem.

I see in the errors you're getting that you're trying to use array_keys, and it's giving you an error message saying you're passing in an object, when typically array_keys is designed to operate on arrays. I think what you want to be doing is $toprated = json_decode($toprated, true); on line 3 in the code above. By default, json_decode spits out an object, not an associative array. If you are wanting to be doing things like array_keys and array_flip and array_merge, you need to be working with an array.

thank you for your reply and your time.
I have tried $toprated = json_decode($toprated, true) before but i get same result in var_dump and Notice: Trying to get property 'results' of non-object on forth line and of course a Warning: Invalid argument supplied for foreach()
this is the resultof vardump:

> object(stdClass)[14]
  public 'page' => int 1
  public 'results' => 
    array (size=20)
      0 => 
        object(stdClass)[35]
          public 'adult' => boolean false
          public 'backdrop_path' => string '/xRI636TOdS1K1GBqIBRSmfZ1T5x.jpg' (length=32)
          public 'genre_ids' => 
            array (size=3)
              ...
          public 'id' => int 19404
          public 'original_language' => string 'hi' (length=2)
          public 'original_title' => string 'दिलवाले दुल्हनिया ले जायेंगे' (length=78)
          public 'overview' => string 'Raj is a rich, carefree, happy-go-lucky second generation NRI. Simran is the daughter of Chaudhary Baldev Singh, who in spite of being an NRI is very strict about adherence to Indian values. Simran has left for India to be married to her childhood fiancé. Raj leaves for India with a mission at his hands, to claim his lady love under the noses of her whole family. Thus begins a saga.' (length=386)
          public 'popularity' => float 17.64
          public 'poster_path' => string '/2CAL2433ZeIihfX1Hb2139CX0pW.jpg' (length=32)
          public 'release_date' => string '1995-10-20' (length=10)
          public 'title' => string 'Dilwale Dulhania Le Jayenge' (length=27)
          public 'video' => boolean false
          public 'vote_average' => float 8.7
          public 'vote_count' => int 2799
      1 => 
        object(stdClass)[36]
          public 'adult' => boolean false
          public 'backdrop_path' => string '/iNh3BivHyg5sQRPP1KOkzguEX0H.jpg' (length=32)
          public 'genre_ids' => 
            array (size=2)
              ...
          public 'id' => int 278
          public 'original_language' => string 'en' (length=2)
          public 'original_title' => string 'The Shawshank Redemption' (length=24)
          public 'overview' => string 'Framed in the 1940s for the double murder of his wife and her lover, upstanding banker Andy Dufresne begins a new life at the Shawshank prison, where he puts his accounting skills to work for an amoral warden. During his long stretch in prison, Dufresne comes to be admired by the other inmates -- including an older prisoner named Red -- for his integrity and unquenchable sense of hope.' (length=388)
          public 'popularity' => float 46.54
          public 'poster_path' => string '/q6y0Go1tsGEsmtFryDOJo3dEmqu.jpg' (length=32)
          public 'release_date' => string '1994-09-23' (length=10)
          public 'title' => string 'The Shawshank Redemption' (length=24)
          public 'video' => boolean false
          public 'vote_average' => float 8.7
          public 'vote_count' => int 18730

Since you are using objects, you can turn them into an array with get_object_vars

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.