【问题标题】:Get response variables from array从数组中获取响应变量
【发布时间】:2020-02-18 01:01:47
【问题描述】:

我正在尝试输出我在 Marqeta API 沙盒环境中创建的所有用户。我正在通过处理每种请求类型的自定义 marqetaAPI 函数向 /users 发出 GET 请求。

调用自定义 API 函数:

$responseData = MarqetaAPI('GET', 'https://sandbox- 
api.marqeta.com/v3/users/');

使用 cURL 调用自定义函数 API:

function marqetaAPI($method, $url, $data, $headers = false) {

//credentials
$uname='xxxxx-xxxxx-xxxx-xxxx';
$pword='xxxxx-xxxxx-xxxx-xxxx';

$curl = curl_init();
switch ($method){
case "POST":
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'POST');
if ($data)
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
break;
case "PUT":
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PUT");
if ($data)
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);                              
break;
default:
if ($data)
$url = sprintf("%s?%s", $url, http_build_query($data));
}
// OPTIONS:
curl_setopt($curl, CURLOPT_URL, $url);
if (!$headers) {
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
// 'APIKEY: $uname:$pword',
'Content-Type: application/json',
));
}else {
curl_setopt($curl, CURLOPT_HTTPHEADER, array (
// 'APIKEY: $uname:$pword',
'Content-Type: application/json',
));
}

curl_setopt($curl, CURLOPT_USERPWD, "$uname:$pword");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
// EXECUTE:
$result = curl_exec($curl);
if(!$result){die("Connection Failure");}
curl_close($curl);
return $result;
}

使用 json_decode 解码响应数组:

$responseArray = json_decode($data, true);

这里是json解码的数组:

Array
(
    [count] => 5
    [start_index] => 0
    [end_index] => 4
    [is_more] => 1
    [data] => Array
        (
            [0] => Array
                (
                    [token] => f7b17921-8f77-4896-82ae-1dc3114ff3e7
                    [active] => 1
                    [uses_parent_account] => 
                    [corporate_card_holder] => 
                    [created_time] => 2020-02-05T15:20:49Z
                    [last_modified_time] => 2020-02-05T15:20:49Z
                    [metadata] => Array
                        (
                        )

                    [account_holder_group_token] => DEFAULT_AHG
                    [status] => ACTIVE
                )

            [1] => Array
                (
                    [token] => 638ab57b-bffa-48a7-938f-eae5f3620bd5
                    [active] => 1
                    [uses_parent_account] => 
                    [corporate_card_holder] => 
                    [created_time] => 2020-02-05T15:19:55Z
                    [last_modified_time] => 2020-02-05T15:19:55Z
                    [metadata] => Array
                        (
                        )

                    [account_holder_group_token] => DEFAULT_AHG
                    [status] => ACTIVE
                )

            [2] => Array
                (
                    [token] => 3242d309-1a53-493e-9bfd-dd6351cbb851
                    [active] => 1
                    [uses_parent_account] => 
                    [corporate_card_holder] => 
                    [created_time] => 2020-02-04T16:24:13Z
                    [last_modified_time] => 2020-02-04T16:24:13Z
                    [metadata] => Array
                        (
                        )

                    [account_holder_group_token] => DEFAULT_AHG
                    [status] => ACTIVE
                )

            [3] => Array
                (
                    [token] => 67a045d1-87b7-40fd-8a0f-9c6b88a7f10b
                    [active] => 1
                    [uses_parent_account] => 
                    [corporate_card_holder] => 
                    [created_time] => 2020-02-04T16:23:51Z
                    [last_modified_time] => 2020-02-04T16:23:51Z
                    [metadata] => Array
                        (
                        )

                    [account_holder_group_token] => DEFAULT_AHG
                    [status] => ACTIVE
                )

            [4] => Array
                (
                    [token] => cb12cd70-a119-42a9-ba22-90b88a85f216
                    [active] => 1
                    [uses_parent_account] => 
                    [corporate_card_holder] => 
                    [created_time] => 2020-02-04T16:17:25Z
                    [last_modified_time] => 2020-02-04T16:17:25Z
                    [metadata] => Array
                        (
                        )

                    [account_holder_group_token] => DEFAULT_AHG
                    [status] => ACTIVE
                )

        )

)

我在这里调用响应数组嵌套的“数据”键。结果是 5 个数组(每个用户一个)

foreach($responseArray['data'] as $key=>$value) {
 echo $key . " " . $value . "<br>";
};

//Result
    0 Array
    1 Array
    2 Array
    3 Array
    4 Array

假设我想要我要做的第一个用户键和值:

foreach($response['data'][0] as $key=>$value) {
    echo $key . " " . $value . "<br>";
};

//Result
token f7b17921-8f77-4896-82ae-1dc3114ff3e7
active 1
uses_parent_account
corporate_card_holder
created_time 2020-02-05T15:20:49Z
last_modified_time 2020-02-05T15:20:49Z
metadata Array
account_holder_group_token DEFAULT_AHG
status ACTIVE

现在假设我想回显每个用户的键和值,我会这样做:

//loop over each $response['data'] array
foreach($response['data'] as $userArrays) {

  //loop over each users keys and values
  foreach($userArrays as $key=>$value) {

    echo $key . " " . $value . "<br>";
  };
};

预期输出:

First User:
token f7b17921-8f77-4896-82ae-1dc3114ff3e7
active 1
uses_parent_account
corporate_card_holder
created_time 2020-02-05T15:20:49Z
last_modified_time 2020-02-05T15:20:49Z
metadata Array
account_holder_group_token DEFAULT_AHG
status ACTIVE

Second User:
token 67a045d1-87b7-40fd-8a0f-9c6b88a7f10b
active 1
uses_parent_account
corporate_card_holder
created_time 2020-02-05T15:20:49Z
last_modified_time 2020-02-05T15:20:49Z
metadata Array
account_holder_group_token DEFAULT_AHG
status ACTIVE

etc.

【问题讨论】:

  • 您能否向我们展示您的阵列的完整结构?还是深度变量?
  • 在不知道$value 包含什么的情况下,很难说出你需要如何输出它。您可以简单地在您的echo 中使用print_r($value, true)
  • “我认为我需要一个嵌套的 foreach,但我不确定它是如何使用键/值完成的。” - 如果 $value 是一个数组,你可以再做一次 foreach:foreach ($value as $itemName =&gt; $itemValue),或者你想调用的变量
  • 只要给我们看print_r($responseArray),我们就知道我们在处理什么。然后向我们展示您想要的结果的示例
  • 我刚刚添加了一个链接-> i.stack.imgur.com/zc6DI.png

标签: php api


【解决方案1】:

如果你只想回显variable depth中的所有数组,那么这个函数是一个解决方案:

function echo_recursive( &$array )  {
  foreach( $array as $key => &$value )  {
    if( is_array( $value ) )  {
      echo_recursive( $value );
    } else {
      echo "$key: $value<br>";
      // or do whatever you want with these
    }
  }
}

// USAGE:
// print_recursive($responseArray);

根据您的上述评论 - echo only one key:

foreach( $responseArray['data'] as $data )  {
  echo "token: " . $data['token'] . "<br>";
  // echo "active: " . $data['active'] . "<br>";
  // echo "created_time: " . $data['created_time'] . "<br>";
  // and so on...
}

或者如果你想echo all the data:

foreach( $responseArray['data'] as $data )  {
  foreach( $data as $key => $value )  {
    echo "$key: $value<br>";
  }
}

【讨论】:

  • 非常感谢!我以为我必须为每个 foreach 循环分配“key=>value”。我想我都错了
  • 递归函数也很有用
猜你喜欢
  • 1970-01-01
  • 2013-07-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-22
  • 1970-01-01
相关资源
最近更新 更多