Kamruddin Bivob Kamruddin Bivob - 3 months ago 8
PHP Question

how to add array without creating a id number?

$url = file_get_contents("http://api.themoviedb.org/3/movie/000008?append_to_response=credits,images&api_key=###");

$json = json_decode($url, true)
$inp = file_get_contents('test3.json');
$tempArray = json_decode($inp,true);
$test = array($title=>$json);
array_push($tempArray, $test);
$jsonData = json_encode($tempArray,JSON_FORCE_OBJECT);
file_put_contents('test3.json', $jsonData);


when i add new array it creates a id number like this - >

"Ariel": {
"adult": false,
"backdrop_path": "\/z2QUexmccqrvw1kDMw3R8TxAh5E.jpg",
"belongs_to_collection": {
"id": 8384,
"name": "Proletariat Collection",
"poster_path": null,
"backdrop_path": "\/ibLeWo3X3dVo4rxvn4m3y90ms1I.jpg"
},
"0": {
"Varjoja paratiisissa": {
"adult": false,
"backdrop_path": "\/6YjUX87VtIEuDzanBE6obVxE9V3.jpg",
"belongs_to_collection": {
"id": 8384,
"name": "Proletariat Collection",
"poster_path": null,
"backdrop_path": "\/ibLeWo3X3dVo4rxvn4m3y90ms1I.jpg"
},
"1": {
"Life in Loops (A Megacities RMX)": {
"adult": false,
"backdrop_path": "\/udvB86uyzJ6P9vmB83WfrCbnmnI.jpg",
"belongs_to_collection": null,
"budget": 42000,
"genres": {
"0": {
"id": 99,
"name": "Documentary"
}
}


first one is ok but then it adds new id number befire movie title .i Dont want to add id number. please can anyone help me how add new array without creating id number?

Sol Sol
Answer
  1. sort() can change the array indices into numbers.
  2. JSON_FORCE_OBJECT may not need.

So:

 array_push($tempArray, $test);
 sort($tempArray);
 $jsonData = json_encode($tempArray);
Comments