php-tmdb/laravel

Convert Collection (ResultCollection, GenricCollection) to Array

roliod opened this issue · 4 comments

Is there a way to convert a Collection back to an Array?

There is always the toArray() method to call on collections, although not having been busy with the code in a very long time I am not sure what results this produces over the various options that give back a collection.

@wtfzdotnet It returns some sort of array but with no iterations you can call, so it is quite difficult to specify values to get.

array:20 [
  "000000003e9fe368000000002c1f0e84" => Movie {#1016
    -adult: false
    -backdropPath: "/o8u0NyEigCEaZHBdCYTRfXR8U4i.jpg"
    -backdrop: BackdropImage {#1041
      -filePath: "/o8u0NyEigCEaZHBdCYTRfXR8U4i.jpg"
      -width: null
      -height: null
      -iso6391: null
      -aspectRatio: null
      -voteAverage: null
      -voteCount: null
      -media: null
      #id: null
      #type: null
    }
    -belongsToCollection: null
    -budget: null
    -genres: Genres {#1047
      #data: array:2 [
        0 => Genre {#1048
          -id: 53
          -name: null
        }
        1 => Genre {#1049
          -id: 27
          -name: null
        }
      ]
    }
    -homepage: null
    -id: 396422
    -imdbId: null
    -originalTitle: "Annabelle: Creation"
    -originalLanguage: "en"
    -overview: "Several years after the tragic death of their little girl, a dollmaker and his wife welcome a nun and several girls from a shuttered orphanage into their home, soon becoming the target of the dollmaker's possessed creation, Annabelle."
    -popularity: 205.817386
    -poster: PosterImage {#1050
      -filePath: "/tb86j8jVCVsdZnzf8I6cIi65IeM.jpg"
      -width: null
      -height: null
      -iso6391: null
      -aspectRatio: null
      -voteAverage: null
      -voteCount: null
      -media: null
      #id: null
      #type: null
    }
    -posterPath: "/tb86j8jVCVsdZnzf8I6cIi65IeM.jpg"
    -productionCompanies: GenericCollection {#1039
      #data: []
    }
    -productionCountries: GenericCollection {#1032
      #data: []
    }
    -releaseDate: DateTime {#1053
      +"date": "2017-08-03 00:00:00.000000"
      +"timezone_type": 3
      +"timezone": "UTC"
    }
    -revenue: null
    -runtime: null
    -spokenLanguages: GenericCollection {#1034
      #data: []
    }
    -status: null
    -tagline: null
    -title: "Annabelle: Creation"
    -voteAverage: 6.4
    -voteCount: 938
    #alternativeTitles: GenericCollection {#1033
      #data: []
    }
    #changes: GenericCollection {#1021
      #data: []
    }
    #credits: CreditsCollection {#1022
      +cast: Cast {#1019
        #data: []
      }
      -crew: Crew {#1018
        #data: []
      }
      -guestStars: GuestStars {#1035
        #data: []
      }
    }
    #images: Images {#1031
      #data: []
    }
    #keywords: GenericCollection {#1020
      #data: []
    }
    #lists: GenericCollection {#1036
      #data: []
    }
    #releases: GenericCollection {#1042
      #data: []
    }
    #release_dates: GenericCollection {#1043
      #data: []
    }
    #similar: GenericCollection {#1044
      #data: []
    }
    #translations: GenericCollection {#1045
      #data: []
    }
    #reviews: null
    #videos: Videos {#1046
      #data: []
    }
  }
  "000000003e9fe48b000000002c1f0e84" => Movie {#1051
    -adult: false
    -backdropPath: "/8eQrIUKFh1Sbpd5r3x2ZWlzqFsr.jpg"
    -backdrop: BackdropImage {#1052
      -filePath: "/8eQrIUKFh1Sbpd5r3x2ZWlzqFsr.jpg"
      -width: null
      -height: null
      -iso6391: null
      -aspectRatio: null
      -voteAverage: null
      -voteCount: null
      -media: null
      #id: null
      #type: null
    }
    -belongsToCollection: null
    -budget: null
    -genres: Genres {#1071
      #data: array:3 [
        0 => Genre {#1072
          -id: 28
          -name: null
        }
        1 => Genre {#1073
          -id: 12
          -name: null
        }
        2 => Genre {#1074
          -id: 35
          -name: null
        }
      ]
    }
    -homepage: null
    -id: 343668
    -imdbId: null
    -originalTitle: "Kingsman: The Golden Circle"
    -originalLanguage: "en"
    -overview: "When an attack on the Kingsman headquarters takes place and a new villain rises, Eggsy and Merlin are forced to work together with the American agency known as the Statesman to save the world."
    -popularity: 181.111484
    -poster: PosterImage {#1075
      -filePath: "/pKESfn2Pdy0b7drvZHQb7UzgqoY.jpg"
      -width: null
      -height: null
      -iso6391: null
      -aspectRatio: null
      -voteAverage: null
      -voteCount: null
      -media: null
      #id: null
      #type: null
    }
    -posterPath: "/pKESfn2Pdy0b7drvZHQb7UzgqoY.jpg"
    -productionCompanies: GenericCollection {#1054
      #data: []
    }
    -productionCountries: GenericCollection {#1055
      #data: []
    }
    -releaseDate: DateTime {#1078
      +"date": "2017-09-20 00:00:00.000000"
      +"timezone_type": 3
      +"timezone": "UTC"
    }
    -revenue: null
    -runtime: null
    -spokenLanguages: GenericCollection {#1056
      #data: []
    }
    -status: null
    -tagline: null
    -title: "Kingsman: The Golden Circle"
    -voteAverage: 7.4
    -voteCount: 163
    #alternativeTitles: GenericCollection {#1057
      #data: []
    }
    #changes: GenericCollection {#1058
      #data: []
    }
    #credits: CreditsCollection {#1059
      +cast: Cast {#1060
        #data: []
      }
      -crew: Crew {#1061
        #data: []
      }
      -guestStars: GuestStars {#1062
        #data: []
      }
    }
    #images: Images {#1063
      #data: []
    }
    #keywords: GenericCollection {#1064
      #data: []
    }
    #lists: GenericCollection {#1065
      #data: []
    }
    #releases: GenericCollection {#1066
      #data: []
    }
    #release_dates: GenericCollection {#1067
      #data: []
    }
    #similar: GenericCollection {#1068
      #data: []
    }
    #translations: GenericCollection {#1069
      #data: []
    }
    #reviews: null
    #videos: Videos {#1070
      #data: []
    }
  }

I think the best way will be to have a reverse of the 'createCollection' function in the Factory.

I don't know your exact use case for wanting this data, but I have some idea's of why you would want this;

Clientside ajax call

I think you are far better off exposing a javascript implementation that directly connects to tmdb.

For other reasons I would honestly just consume the raw api methods and skip the overhead of creating the models etc.

Hope that helps, if not being more specific might help :)

@wtfzdotnet I wanted to use this package to create an API quite similar to that of TMDB itself.

So I'll need the collection in an Array so that I can easily convert to json using the json_encode() function. The toArray() function on the collection does not return a valid array so json_encode() returns as empty.

I found a work around though. But I had to pick the values I wanted from the collection, convert to an array and then to json. I just thought it might be best to have a function that can be used to achieve this