从数组中获取响应变量

时间:2020-02-17 15:46:47

标签: php api

我正在尝试输出在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.

1 个答案:

答案 0 :(得分:0)

如果只想回显df = pd.read_csv(r'path/to/myfile.csv', sep=';') df.groupby(['id', 'name'])['unitCount', 'orderCount', 'invoiceCount'] \ .sum() \ .rename(columns={'unitCount':'total_unitCount', 'orderCount' : 'total_orderCount', 'invoiceCount': 'total_invoiceCount'}) \ .to_csv('path/to/myoutputfile_sum.csv', sep=';') 中的所有数组,则此函数是一种解决方案:

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