如何在php中存储firebase返回的第一个数组的所有值

如何在php中存储firebase返回的第一个数组的所有值,php,json,firebase,firebase-realtime-database,Php,Json,Firebase,Firebase Realtime Database,假设我在dbuser { "signup_login" : { "2315218123" : { "name" : "yohan", }, "2665451854" : { "name" : "Ram", }, "5453698463" : { "name" : "Raj", } } } class lol { protected $database; protected $dbnam

假设我在dbuser

{
  "signup_login" : {
    "2315218123" : {
      "name" : "yohan",
    },
    "2665451854" : {
      "name" : "Ram",
    },
    "5453698463" : {
      "name" : "Raj",
    }
  }
}
class lol  {
    protected $database;
    protected $dbname = 'signup_login';

    public function __construct(){
        $acc = ServiceAccount::fromJsonFile(__DIR__ . '/secret/user-key.json');
        $firebase = (new Factory)->withServiceAccount($acc)->create();

        $this->database = $firebase->getDatabase();
    }

    public function get(){

        if ($this->database->getReference($this->dbname)->getSnapshot()){
            return $this->database->getReference($this->dbname)->getValue();
        } else {
            return FALSE;
        }
    }
}

$users = new lol();
var_dump($users->get());
因为我尝试了这段代码来检索数据库user下的signup\u login数据

{
  "signup_login" : {
    "2315218123" : {
      "name" : "yohan",
    },
    "2665451854" : {
      "name" : "Ram",
    },
    "5453698463" : {
      "name" : "Raj",
    }
  }
}
class lol  {
    protected $database;
    protected $dbname = 'signup_login';

    public function __construct(){
        $acc = ServiceAccount::fromJsonFile(__DIR__ . '/secret/user-key.json');
        $firebase = (new Factory)->withServiceAccount($acc)->create();

        $this->database = $firebase->getDatabase();
    }

    public function get(){

        if ($this->database->getReference($this->dbname)->getSnapshot()){
            return $this->database->getReference($this->dbname)->getValue();
        } else {
            return FALSE;
        }
    }
}

$users = new lol();
var_dump($users->get());
因为我想以数组的形式存储数据,比如

$data = ["2315218123","2665451854","5453698463"]
但是上面写的代码将其作为输出返回,但我想将其存储在上面提到的数组中,但我无法这样做

array(3) {
  [2315218123]=>
  array(1) {
    ["name"]=>
    string(5) "yohan"
  }
  [2665451854]=>
  array(1) {
    ["name"]=>
    string(3) "Ram"
  }
  [5453698463]=>
  array(1) {
    ["name"]=>
    string(3) "Raj"
  }
 }


按照您的示例,您可以通过以下方式获取ID:

$lol=新lol();
$userIds=array_key($users->get());
var_dump($userid);
PHP在使用数组时会将整数字符串转换为整数,因此之后必须将值转换为字符串:

$userIds=array\u map('strval',$userIds);
一些注意事项:

显然,您正在使用,但使用的方法已被弃用很长时间。我建议您更新到最新版本(撰写本文时为4.40),并遵循上的文档

而不是写作

$acc=ServiceAccount::fromJsonFile(_DIR__.'./secret/user key.json');
$firebase=(新工厂)->withServiceAccount($acc)->create();
$database=$firebase->getDatabase();
你应该使用

$acc=\uuuuuuu DIR\uuuuuuuuuuuuuuuuuuuuuuuuuu/secret/user key.json';
$factory=(新工厂)->带有ServiceAccount($acc);
$database=$factory->createDatabase();
在您的示例中,分别调用
getSnapshot()
getValue()
——请注意,这将导致对Firebase API的两个单独调用(
Database::getValue()
只是
Database::getSnapshot()->getValue()
的一个方便快捷方式)

检索引用中的键的更有效方法是使用
reference::getChildKeys()
方法

鉴于所有这些,我建议您将
lol
类重写为如下内容:

class LolWut
{
    /** @var \Kreait\Firebase\Database\Reference */
    private $ref;

    public function __construct($refName = 'signup_login')
    {
        $this->ref = (new Factory())
            ->withServiceAccount(__DIR__ . '/secret/user-key.json')
            ->createDatabase()
            ->getReference($refName); // Does not invoke a call to the Firebase APIs
    }

    public function getUsers()
    {
        return $this->ref->getSnapshot()->getValue();
    }

    /**
     * @return string[]
     */
    public function getUserIds(): array
    {
        return array_map('strval', $this->ref->getChildKeys());
    }
}
最好在类外部实例化工厂,只向类传递内部操作所需的内容

我希望这有帮助