Skip to content
Advertisement

JSON does not show id from DB

I returned the data from my firebase database in a JSON file with PHP CURL but it doesn’t show me the id of the records

my code:

<?php
$url="https://******.firebaseio.com/a_info.json";
$ch= curl_init();
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json')); 
$response=curl_exec($ch);
curl_close($ch);
print_r($response);
?> 

my data base

enter image description here

Advertisement

Answer

That’s because Firebase interprets your sequential, numerical keys as array indices, and returns the results as a JSON array.

If you want to prevent Firebase from performing this conversion, make sure your keys aren’t sequential numbers, for example by prefixing them with a short fixed string:

"a_info": {
  "key0": {...},
  "key1": {...},
  ...
}
}

Also see: Best Practices: Arrays in Firebase.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement