我正在寻找一种解决方案,用于从类属性中的PHP数组中读取数据。
请求:
$accounts = $client->getAccounts();
这是结果数组:
Coinbase\Wallet\Resource\ResourceCollection Object
(
[previousUri:Coinbase\Wallet\Resource\ResourceCollection:private] =>
[nextUri:Coinbase\Wallet\Resource\ResourceCollection:private] =>
[resources:Coinbase\Wallet\Resource\ResourceCollection:private] => Array
(
[0] => Coinbase\Wallet\Resource\Account Object
(
[name:Coinbase\Wallet\Resource\Account:private] => BTC Wallet
[primary:Coinbase\Wallet\Resource\Account:private] => 1
[type:Coinbase\Wallet\Resource\Account:private] => wallet
[currency:Coinbase\Wallet\Resource\Account:private] => BTC
[balance:Coinbase\Wallet\Resource\Account:private] => Coinbase\Wallet\Value\Money Object
(
[amount:Coinbase\Wallet\Value\Money:private] => 0.00001
[currency:Coinbase\Wallet\Value\Money:private] => BTC
)
[nativeBalance:Coinbase\Wallet\Resource\Account:private] => Coinbase\Wallet\Value\Money Object
(
[amount:Coinbase\Wallet\Value\Money:private] => 0.01
[currency:Coinbase\Wallet\Value\Money:private] => CHF
)
[createdAt:Coinbase\Wallet\Resource\Account:private] => DateTime Object
(
[date] => 2017-11-04 01:35:13.000000
[timezone_type] => 2
[timezone] => Z
)
[updatedAt:Coinbase\Wallet\Resource\Account:private] => DateTime Object
(
[date] => 2017-11-04 14:24:11.000000
[timezone_type] => 2
[timezone] => Z
)
[id:Coinbase\Wallet\Resource\Resource:private] => XXXXXXXXX-XXXXXXXXXX-XXXXXXXXXX
[resource:Coinbase\Wallet\Resource\Resource:private] => account
[resourcePath:Coinbase\Wallet\Resource\Resource:private] => /v2/accounts/XXXXXXXXX-XXXXXXXXXX-XXXXXXXXXX
[rawData:Coinbase\Wallet\Resource\Resource:private] => Array
(
[id] => XXXXXXXXX-XXXXXXXXXX-XXXXXXXXXX
[name] => BTC Wallet
[primary] => 1
[type] => wallet
[currency] => BTC
[balance] => Array
(
[amount] => 0.001
[currency] => BTC
)
[created_at] => 2017-11-04T01:35:13Z
[updated_at] => 2017-11-04T14:24:11Z
[resource] => account
[resource_path] => /v2/accounts/XXXXXXXXX-XXXXXXXXXX-XXXXXXXXXX
[native_balance] => Array
(
[amount] => 0.01
[currency] => CHF
)
)
)
)
)
现在,我想读取$accounts['rawData']['id']
,其值为XXXXXXXXX-XXXXXXXXXX-XXXXXXXXXX
我已经尝试将这些响应转换为这样的JSON数组:
$aaax = json_encode($accounts);
$bbbx = json_decode($aaax, true);
但是它不起作用。上
print_r($bbbx)
我只收到
Array
(
)
您知道我如何从输出数组读取/获取数据吗?
答案 0 :(得分:0)
问题是您班级的所有属性都具有private
visibility。这意味着您在课程之外的代码无法读取或修改这些属性。
您需要修改类以使代码可访问这些属性,或者至少在运行json_encode()
时告诉PHP包括私有属性。假设您不能这样做,因为这是一个库,而不是您编写的代码,则需要将该类包装在实现JsonSerializable
的包装器类中。
答案 1 :(得分:0)
感谢Ed Cottrell的帮助以及技术术语的更正。那是我学习的最好方法。我试图用JsonSerializable :: jsonSerialize解决这个问题。没办法(对我来说...)
现在,我找到了另一种访问数据的解决方案。我使用php包装器: https://github.com/coinbase/coinbase-php
有一种方法可以访问RawData
$data = $client->decodeLastResponse();
就我而言,以下代码:
$accounts = $client->getAccounts();
$dataX = $client->decodeLastResponse();
print_r($dataX);
所以我得到以下响应:
Array
(
[pagination] => Array
(
[ending_before] =>
[starting_after] =>
[limit] => 25
[order] => desc
[previous_uri] =>
[next_uri] =>
)
[data] => Array
(
[0] => Array
(
[id] => XXXXXXXXXXXXXXXXXXX
[name] => BTC Wallet
[primary] => 1
[type] => wallet
[currency] => BTC
[balance] => Array
(
[amount] => 0.001
[currency] => BTC
)
[created_at] => 2017-11-04T01:35:13Z
[updated_at] => 2017-11-04T14:24:11Z
[resource] => account
[resource_path] => /v2/accounts/XXXXXXXXXXXXXXXXXXX
[native_balance] => Array
(
[amount] => 0.001
[currency] => CHF
)
)
)
)
因此完全可以访问 再次感谢您(让我敞开心opening .. :-))我用Google搜索“ coinbase jsonserialize”找到了这个解决方案...这是您的输入。