Accommodate JSON data with PHP

0

I have some problems to accommodate data obtained from an API, they come in "JSON" format, try to accommodate them using an "array" but I did not succeed.

The data that the API throws at me when making a "Get" are the following:

{

    "page_count": "integer",
    "page_number": "integer",
    "page_size": "integer",
    "total_records": "integer",
    "accounts": [
        {
            "id": "string [uuid]",
            "account_name": "string",
            "email": "string",
            "account_type": "string",
            "subscription_start_time": "string [date-time]",
            "subscription_end_time": "string [date-time]",
            "created_at": "string [date-time]"
        }
    ]
}

Based on the previous Code, I would like to obtain the "ID, Account_Name, Email, Account_Type" only for each account.

Could someone give me an example?

Thanks, regards!

    
asked by Stone Garden 13.06.2018 в 18:24
source

1 answer

2

Hello, good morning everyone,

I commented that I already found how to accommodate the information that the API throws in JSON format, likewise the attached below, I hope it serves someone else.

$response = curl_exec($ch); // En la variable response se encuentra guardado el URL del API "Get"
$response = json_decode($response, true);

foreach($response['accounts'] as $item) {
print '<strong>ID:</strong> ' . $item['id'];
print ' - ';
print '<strong>Nombre de la cuenta:</strong> ' . $item['account_name'];
print ' - ';
print '<strong>Email:</strong> ' . $item['email'];
print ' - ';
print '<strong>Tipo de cuenta:</strong> ' . $item['account_type'];
print '<br>';
}

Greetings!

    
answered by 19.06.2018 в 16:39