PHP对象,其中包含查找键/值的对象数组,并将其与另一个键/值对链接

时间:2016-05-24 07:36:31

标签: php object multidimensional-array key coinbase-php

进展:差不多......最后一点需要帮助!!!

$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]我们的示例&#39; iLy&#39;搜索关键值&#39; id&#39;,以获取$ accountId。

理想情况下,我可以搜索&#39; name&#39; =&GT; iLy并让它返回&#39; id&#39; =&GT; &#39; XXXXXXX&#39;在同一个数组中。

当我创建帐户时,这是我能够弄清楚如何获取帐户ID的唯一方法,而且我还没有找到任何其他API调用来获取帐户的名称。我只能使用$users[2]隔离一个对象。我知道这可能是一个重复的帖子,但我还没有能够使任何其他解决方案工作。 PHP - find entry by object property from a array of objectsArray of PHP Objects

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

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

返回:

&#13;
&#13;
    

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
)
)
)
)
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分: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循环。