【问题标题】:How to store all the value of 1st array returned by firebase in php如何在php中存储firebase返回的第一个数组的所有值
【发布时间】:2020-06-06 13:23:56
【问题描述】:

假设我在 db user

下有数据
{
  "signup_login" : {
    "2315218123" : {
      "name" : "yohan",
    },
    "2665451854" : {
      "name" : "Ram",
    },
    "5453698463" : {
      "name" : "Raj",
    }
  }
}

因为我已经尝试过这段代码来检索数据库 user

下的 signup_login 的数据
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"
  }
 }

【问题讨论】:

    标签: php json firebase firebase-realtime-database


    【解决方案1】:

    按照您的示例,您可以获得带有 array_keys() 的 ID:

    $lol = new lol();
    $userIds = array_keys($users->get());
    var_dump($userIds);
    

    PHP 在使用数组时会将整数字符串转换为整数,因此您必须在之后将值转换为字符串:

    $userIds = array_map('strval', $userIds);
    

    一些注意事项:

    您显然在使用Firebase Admin SDK for PHP,但使用的方法早已被弃用。我建议您更新到最新版本(撰写本文时为 4.40)并按照https://firebase-php.readthedocs.io/ 上的文档进行操作。

    而不是写

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

    你应该使用

    $acc = __DIR__ . '/secret/user-key.json';
    $factory = (new Factory)->withServiceAccount($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());
        }
    }
    

    在你的类之外实例化工厂也是一个好主意,并且只将你需要在其中操作的东西传递给你的类。

    我希望这会有所帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-11-30
      • 1970-01-01
      • 2018-05-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-17
      • 1970-01-01
      相关资源
      最近更新 更多