1

进展:快到了……最后一点需要帮助!!!

$users = $client->getAccounts();


$num = count($users);
for ( $x=0; $x < $num; $x++){
    foreach($users[$x] as $y => $y_value) {
        if($y_value == iLy){
            print_r($y);
            echo '<br>';
            print_r($x);
            echo '<br>';
            print_r($user[$x]["id"]);
        }
    }
}

以上是我编写的最新代码,我一直在通过按名称搜索来定位数组值或要查找的数组。print_r($user[$x]["id"]); 结果是:

Warning: Illegal string offset 'id' in /home/examplesite/BLANK.com/CBapi.php on line 37

我使用这种简化来发现我的解决方案的逻辑: 这行得通

<?php
$age = array (array("name"=>"iLy", "id"=>"37", "balance"=>"43"),
              array("name"=>"jim", "id"=>"67", "balance"=>"47"));

echo $age[0]['id'];

$num = count($age);
for ( $x=0; $x < $num; $x++){
    foreach($age[$x] as $y => $y_value) {
        if($y_value == 43){
            print_r($y);
            echo '<br>';
            print_r($x);
            echo '<br>';
            print_r($age[$x]["id"]);

        }
    }
}
?>

我相信我只是调用了错误的键名。
++++++++++++++++++++++++下面的原帖++++++++++++++++++++++++

当前代码:

$configuration = Configuration::apiKey($apiKey, $apiSecret);
$configuration->setApiUrl(Configuration::SANDBOX_API_URL); 
$client = Client::create($configuration);

$users = $client->getAccounts();

我已经为此苦苦挣扎了四天......我使用了下面的函数,它返回一个包含对象数组的对象。我的目标是在对象集合中搜索键和值,(例如,搜索当前用户的姓名:$currentuser = 'iLy'; $user = $currentuser;然后我可以识别数组值, $users[2]对于我们的示例“iLy”,它可以搜索键值'id',获取 $accountId。

理想情况下,我可以搜索 'name' => iLy 并让它在同一个数组中返回 'id' => 'xxxxxxx'。

当我创建帐户时,这是我能够弄清楚如何获取 accountID 的唯一方法,而且我还没有找到任何其他 API 调用来按名称获取帐户。我只能使用$users[2]. 我知道这可能是一个重复的帖子,但我无法使任何其他解决方案起作用。 PHP - 从对象数组,PHP对象数组中按对象属性查找条目

也许我使用了错误的解决方案或错误地应用了它们。任何帮助将不胜感激!

public function getAccounts(array $params = [])
{
    return $this->getAndMapCollection('/v2/accounts',$params,'toAccounts');                           
}

返回这个:

    

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] => Jennaod3
[primary:Coinbase\\Wallet\\Resource\\Account:private] =>
[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.00000000
[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.00
[currency:Coinbase\\Wallet\\Value\\Money:private] => USD
)
[createdAt:Coinbase\\Wallet\\Resource\\Account:private] => DateTime Object (
[date] => 2016-05-24 04:55:41.000000
[timezone_type] => 2
[timezone] => Z
)
[updatedAt:Coinbase\\Wallet\\Resource\\Account:private] => DateTime Object (
[date] => 2016-05-24 04:55:41.000000
[timezone_type] => 2
[timezone] => Z
)
[id:Coinbase\\Wallet\\Resource\\Resource:private] => 0d41fc45-0a53-58cb-9931-c9a33f520963
[resource:Coinbase\\Wallet\\Resource\\Resource:private] => account
[resourcePath:Coinbase\\Wallet\\Resource\\Resource:private] => /v2/accounts/0d41fc45-0a53-58cb-9931-c9a33f520963
[rawData:Coinbase\\Wallet\\Resource\\Resource:private] => Array (
[id] => 0d41fc45-0a53-58cb-9931-c9a33f520963
[name] => Jennaod3
[primary] =>
[type] => wallet
[currency] => BTC
[balance] => Array (
[amount] => 0.00000000
[currency] => BTC
)
[native_balance] => Array (
[amount] => 0.00
[currency] => USD
)
[created_at] => 2016-05-24T04:55:41Z
[updated_at] => 2016-05-24T04:55:41Z
[resource] => account
[resource_path] => /v2/accounts/0d41fc45-0a53-58cb-9931-c9a33f520963
)
)
[1] => Coinbase\\Wallet\\Resource\\Account Object (
[name:Coinbase\\Wallet\\Resource\\Account:private] => jenna works to
[primary:Coinbase\\Wallet\\Resource\\Account:private] =>
[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.00000000
[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.00
[currency:Coinbase\\Wallet\\Value\\Money:private] => USD
)
[createdAt:Coinbase\\Wallet\\Resource\\Account:private] => DateTime Object (
[date] => 2016-05-22 13:37:16.000000
[timezone_type] => 2
[timezone] => Z
)
[updatedAt:Coinbase\\Wallet\\Resource\\Account:private] => DateTime Object (
[date] => 2016-05-22 13:37:16.000000
[timezone_type] => 2
[timezone] => Z
)
[id:Coinbase\\Wallet\\Resource\\Resource:private] => e7ab48b4-bc76-513a-a78b-6d627f32f848
[resource:Coinbase\\Wallet\\Resource\\Resource:private] => account
[resourcePath:Coinbase\\Wallet\\Resource\\Resource:private] => /v2/accounts/e7ab48b4-bc76-513a-a78b-6d627f32f848
[rawData:Coinbase\\Wallet\\Resource\\Resource:private] => Array (
[id] => e7ab48b4-bc76-513a-a78b-6d627f32f848
[name] => jenna works to
[primary] =>
[type] => wallet
[currency] => BTC
[balance] => Array (
[amount] => 0.00000000
[currency] => BTC
)
[native_balance] => Array (
[amount] => 0.00
[currency] => USD
)
[created_at] => 2016-05-22T13:37:16Z
[updated_at] => 2016-05-22T13:37:16Z
[resource] => account
[resource_path] => /v2/accounts/e7ab48b4-bc76-513a-a78b-6d627f32f848
)
)
[2] => Coinbase\\Wallet\\Resource\\Account Object (
[name:Coinbase\\Wallet\\Resource\\Account:private] => iLy
[primary:Coinbase\\Wallet\\Resource\\Account:private] =>
[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.00000000
[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.00
[currency:Coinbase\\Wallet\\Value\\Money:private] => USD
)
[createdAt:Coinbase\\Wallet\\Resource\\Account:private] => DateTime Object (
[date] => 2016-05-22 13:33:36.000000
[timezone_type] => 2
[timezone] => Z
)
[updatedAt:Coinbase\\Wallet\\Resource\\Account:private] => DateTime Object (
[date] => 2016-05-22 13:33:36.000000
[timezone_type] => 2
[timezone] => Z
)
[id:Coinbase\\Wallet\\Resource\\Resource:private] => c95fd701-cf2b-56f7-b438-9a2f0e61b21c
[resource:Coinbase\\Wallet\\Resource\\Resource:private] => account
[resourcePath:Coinbase\\Wallet\\Resource\\Resource:private] => /v2/accounts/c95fd701-cf2b-56f7-b438-9a2f0e61b21c
[rawData:Coinbase\\Wallet\\Resource\\Resource:private] => Array (
[id] => c95fd701-cf2b-56f7-b438-9a2f0e61b21c
[name] => iLy
[primary] =>
[type] => wallet
[currency] => BTC
[balance] => Array (
[amount] => 0.00000000
[currency] => BTC
)
[native_balance] => Array (
[amount] => 0.00
[currency] => USD
)
[created_at] => 2016-05-22T13:33:36Z
[updated_at] => 2016-05-22T13:33:36Z
[resource] => account
[resource_path] => /v2/accounts/c95fd701-cf2b-56f7-b438-9a2f0e61b21c
)
)
[3] => 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.10000000
[currency:Coinbase\\Wallet\\Value\\Money:private] => BTC
)
[nativeBalance:Coinbase\\Wallet\\Resource\\Account:private] => Coinbase\\Wallet\\Value\\Money Object (
[amount:Coinbase\\Wallet\\Value\\Money:private] => 1000.00
[currency:Coinbase\\Wallet\\Value\\Money:private] => USD
)
[createdAt:Coinbase\\Wallet\\Resource\\Account:private] => DateTime Object (
[date] => 2016-05-21 02:41:13.000000
[timezone_type] => 2
[timezone] => Z
)
[updatedAt:Coinbase\\Wallet\\Resource\\Account:private] => DateTime Object (
[date] => 2016-05-21 02:41:14.000000
[timezone_type] => 2
[timezone] => Z
)
[id:Coinbase\\Wallet\\Resource\\Resource:private] => 0e0dac44-6900-59e9-8183-99b9459d1205
[resource:Coinbase\\Wallet\\Resource\\Resource:private] => account
[resourcePath:Coinbase\\Wallet\\Resource\\Resource:private] => /v2/accounts/0e0dac44-6900-59e9-8183-99b9459d1205
[rawData:Coinbase\\Wallet\\Resource\\Resource:private] => Array (
[id] => 0e0dac44-6900-59e9-8183-99b9459d1205
[name] => BTC Wallet
[primary] => 1
[type] => wallet
[currency] => BTC
[balance] => Array (
[amount] => 0.10000000
[currency] => BTC
)
[native_balance] => Array (
[amount] => 1000.00
[currency] => USD
)
[created_at] => 2016-05-21T02:41:13Z
[updated_at] => 2016-05-21T02:41:14Z
[resource] => account
[resource_path] => /v2/accounts/0e0dac44-6900-59e9-8183-99b9459d1205
)
)
)
)

4

1 回答 1

0

这是我的答案,它并不漂亮。事实证明,我必须在 API 资源文件中公开一些变量,我不确定这是否是个好主意,但它使用以下代码实现了我的目标:

$users = $client->getAccounts();

$num = count($users);

for ( $x=0; $x < $num; $x++){
    foreach($users[$x] as $y => $y_value) {
        if($y_value == iLy) {
            $bae = $users[$x];
            $account_id = '';
            foreach ($bae as $k => $k_value) {
                $account_id = $k_value;
            }           
        }
    }
}

似乎我的所有数组都是对象,而获得所需值的唯一方法是使用 foreach 循环。

于 2016-05-25T02:43:21.727 回答