I am trying to output all the users I have created in my Marqeta API Sandbox environment. I’m making a GET request to /users through my custom marqetaAPI function that handles each request type.
Call to custom API function:
$responseData = MarqetaAPI('GET', 'https://sandbox- api.marqeta.com/v3/users/');
Custom function API Call with cURL:
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; }
Decoding response array with json_decode:
$responseArray = json_decode($data, true);
Here is the json decoded array:
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 ) ) )
I’m calling the response array nested ‘data’ key here. The result is 5 arrays (one for each user)
foreach($responseArray['data'] as $key=>$value) { echo $key . " " . $value . "<br>"; }; //Result 0 Array 1 Array 2 Array 3 Array 4 Array
Say I wanted the first users keys and values I would do:
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
Now lets say i want to echo each users keys and values I would do something like:
//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>"; }; };
with expected output:
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.
Advertisement
Answer
If you only want to echo all arrays in variable depth
, then this function is a solution:
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);
Based on your comment above – 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... }
Or if you want to echo all the data
:
foreach( $responseArray['data'] as $data ) { foreach( $data as $key => $value ) { echo "$key: $value<br>"; } }