So, the keys of the movie AND music in the resulting json get because they themselves and write them there. To get rid of these string keys, simply add elements to the array:
$data[] = $this->Play_lib->getmovie($id); $data[] = $this->Play_lib->getmusic($id);
The next question is where the "movie": [ { } ] nested brackets come from "movie": [ { } ] . The brackets indicate that the result of the getmovie/getmusic are arrays, which in this case consist of 1 element.
In appearance, the call to getmovie($id) is similar to retrieving a single element at the specified ID. Here you can see better, and if the method should return a single element, then change it so that it is, and the object is returned, and not an array of one object.
On the other hand, misunderstanding is possible when id is an identifier of, for example, a genre, and getmusic returns a list of relevant songs.
In any case, if you cannot change the above methods, and from the result of the work of the methods you need only one (or only) element, then your code should take the following form:
$data[] = array_shift($this->Play_lib->getmovie($id)); $data[] = array_shift($this->Play_lib->getmovie($id)); echo json_encode($data);
The used array_shift() method returns the first element of the input array, or null .
$data['movie']replace with$data[] = ..., withmusicsimilarly - terangetMovieandgetMusicreturn an array of 1 element, and not just an object. If it is implied that the method should return only one element, and you can change these methods, then change. If you can not change, then you must extract the first (zero) element of the array, after calling the methods. Tritely, you can write it in the form$this->Play_lib->getmovie($id)[0]but this is a bad way, because If an empty array is returned, this will result in an error. - teran$x = $this->Play_lib->getmovie($id)), and then check that there are more elements than 1 (if(count($x))get the first ($x[0]) However, it is not clear what should be done with the remaining elements of the array, if they exist. - teran