I am trying to create an array when im using TMDB(The movie database) API, I am using this wrapper for PHP.
I am trying to do this
require_once('../classes/tmdb-api.php');
// if you have no $conf it uses the default config
$tmdb = new TMDB();
//Insert your API Key of TMDB
//Necessary if you use default conf
$tmdb->setAPIKey('myKEy');
//Title to search for
$title = $_POST['searchTerm'];
$movies = $tmdb->searchMovie($title);
// returns an array of Movie Object
$movieArray = array();
$movieSearchArray = array();
foreach($movies as $movie) {
$movieTitle = $movie->getTitle();
$movieSearchArray[$movieTitle] = ['ID'][$movie->getID()];
$movieSearchArray[$movieTitle] = ['Trailer'][$movie->getTrailer()];
}
$movieArray[] = $movieSearchArray;
print_r($movieArray);
So for example when a user searches 'Rocky' It will bring back multiple different movie titles but for each of the movie titles I want to create an array like this for example, I am trying to do this in the foreach loop but what am i doing wrong?
[Rocky] => (
"id" = "2",
"rating" = "4"
)
Keep in mind that there could be 10s of different movies. This is what i get returned when I search 'Rocky' right now.
Array
(
[0] => Array
(
[Rocky] =>
[Rocky II] =>
[Rocky IV] =>
[Rocky V] =>
[Rocky Balboa] =>
[Rocky III] =>
[Rocky VI] =>
[Creed] =>
[The Real Rocky] =>
[Rocky Marciano] =>
[Where is Rocky II?] =>
[Rocky Handsome] =>
[Rocky Road] =>
[Rocky Mountain] =>
[Rocky Pink] =>
[Rocky and Bullwinkle] =>
[The Rocky Horror Picture Show] =>
[Rocky Mountain Grandeur] =>
)
)
I guess you can do that, with only exception of assigning item to an array, indices in [] must be before equal (=) sign, or you have to assign whole array with keys and values.
so either
foreach($movies as $movie) {
$movieTitle = $movie->getTitle();
$movieSearchArray[$movieTitle]['ID'] = $movie->getID();
$movieSearchArray[$movieTitle]['Trailer'] = $movie->getTrailer();
}
or
foreach($movies as $movie) {
$movieSearchArray[$movie->getTitle()] = array(
'ID' => $movie->getID(),
'Trailer' => $movie->getTrailer()
);
}
and so on for other data in the $movie object
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.