Fastest way to reduce the runtime is by getting snapshot of the complete database.
This is the hierarchy of database looks like
Database hierarchy in firebase
Below is the basic code to retrieve the data using custom function downloadDatabase();
require __DIR__.'/vendor/autoload.php';
use Kreait\Firebase\Factory;
use Kreait\Firebase\ServiceAccount;
class accountInfo{
protected $database;
protected $dbname = "users";
public function __construct(){
$serviceAccount = ServiceAccount::fromJsonFile(__DIR__.'/secret/yoursecret.json');
$firebase = (new Factory)
->withServiceAccount($serviceAccount)
->create();
$this->database = $firebase->getDatabase();
}
public function downloadDatabase(){
return $this->database->getReference($this->dbname)->getValue();
}
}
$users = new accountInfo();
$array = $users->downloadDatabase();
Now you have your $array with complete database snapshot. You can easily display all the data neatly by using foreach loop.
foreach ($array as $key => $value) {
echo $key."<br>";
if(is_array($value)){
$value2 = $value;
foreach ($value2 as $key2 => $value2) {
echo "----";
echo $key2." : ".$value2."<br>";
}
}
echo "<hr>";
}
To test the speed of your code use this at the beginning of your code.
$start = microtime(true);
and then at the end of your document use this
$time_elapsed_in_microsecs = microtime(true) - $start;
echo "<hr>Runtime : ".$time_elapsed_in_microsecs;