Danilo Paulino da Silva Danilo Paulino da Silva - 5 months ago 39
JSON Question

PHP multidimensional array x HasOffers

i'm stuck a few days with an array. I'm using the HasOffers plataform, i dont know if somewhone knows it but thats the problem.

I'm mading a API Call to export some data from the database. I export ID and NAME for now, they came in a array in a cURL/JSON function. I see the multidimensional array in a print_r() function. But when i try to put into a loop, just don't work.

This is the code of API call:

<?php
// Specify API URL
define('HASOFFERS_API_URL', 'https://api.hasoffers.com/Apiv3/json');

// Specify method arguments
$args = array(
'NetworkId' => 'xxxxx',
'Target' => 'Offer',
'Method' => 'findAll',
'NetworkToken' => 'xxxx',
'fields' => array(
'id',
'name'
)
);

// Initialize cURL
$curlHandle = curl_init();

// Configure cURL request
curl_setopt($curlHandle, CURLOPT_URL, HASOFFERS_API_URL . '?' . http_build_query($args));

// Make sure we can access the response when we execute the call
curl_setopt($curlHandle, CURLOPT_RETURNTRANSFER, true);

// Execute the API call
$jsonEncodedApiResponse = curl_exec($curlHandle);

// Ensure HTTP call was successful
if($jsonEncodedApiResponse === false) {
throw new \RuntimeException(
'API call failed with cURL error: ' . curl_error($curlHandle)
);
}

// Clean up the resource now that we're done with cURL
curl_close($curlHandle);

// Decode the response from a JSON string to a PHP associative array
$apiResponse = json_decode($jsonEncodedApiResponse, true);

// Make sure we got back a well-formed JSON string and that there were no
// errors when decoding it
$jsonErrorCode = json_last_error();
if($jsonErrorCode !== JSON_ERROR_NONE) {
throw new \RuntimeException(
'API response not well-formed (json error code: ' . $jsonErrorCode . ')'
);
}

// Print out the response details
if($apiResponse['response']['status'] === 1) {
// No errors encountered
//echo 'API call successful';
echo PHP_EOL;
echo print_r($apiResponse['response']['data'], true);
echo PHP_EOL;
}
else {
// An error occurred
echo 'API call failed (' . $apiResponse['response']['errorMessage'] . ')';
echo PHP_EOL;
echo 'Errors: ' . print_r($apiResponse['response']['errors'], true);
echo PHP_EOL;
}


And this is part of the result of this call above:

Array ( [521] => Array ( [Offer] => Array ( [id] => 521 [name] => 360 Imprimir - BR ) ) [663] => Array ( [Offer] => Array ( [id] => 663 [name] => 360 Imprimir - BR ) ) [627] => Array ( [Offer] => Array ( [id] => 627 [name] => 360 Imprimir Venda Cartões e Folder - BR ) ) [1048] => Array ( [Offer] => Array ( [id] => 1048 [name] => Aasha - IN ) ) [412] => Array ( [Offer] => Array ( [id] => 412 [name] => ABA - English ) ) [573] => Array ( [Offer] => Array ( [id] => 573 [name] => Aba English - BR ) ) [581] => Array ( [Offer] => Array ( [id] => 581 [name] => Aba English - IT ) ) [575] => Array ( [Offer] => Array ( [id] => 575 [name] => Aba English ES ) ) [579] => Array ( [Offer] => Array ( [id] => 579 [name] => Aba English FR ) ) [577] => Array ( [Offer] => Array ( [id] => 577 [name] => Aba English MX ) ) [224] => Array ( [Offer] => Array ( [id] => 224 [name] => Abrinq ) ) [513] => Array ( [Offer] => Array ( [id] => 513 [name] => Acelerador Digital - BR ) ) [218] => Array ( [Offer] => Array ( [id] => 218 [name] => ActionAid - Apadrinhe Uma Criança ) ) [168] => Array ( [Offer] => Array ( [id] => 168 [name] => ActionAid - Apadrinhe uma criança ) ) [496] => Array ( [Offer] => Array ( [id] => 496 [name] => ActionAid - Apadrinhe Uma Criança Display ) ) [718] => Array ( [Offer] => Array ( [id] => 718 [name] => Adrian - US ) ) [374] => Array ( [Offer] => Array ( [id] => 374 [name] => Agarre ) ) [442] => Array ( [Offer] => Array ( [id] => 442 [name] => Agarre Brasil ) ) [676] => Array ( [Offer] => Array ( [id] => 676 [name] => Aladim Shop - BR ) ) [1050] => Array ( [Offer] => Array ( [id] => 1050 [name] => Alisha - IN ) ) [1259] => Array ( [Offer] => Array ( [id] => 1259 [name] => Alternativa de inversion - ES ) ) [844] => Array ( [Offer] => Array ( [id] => 844 [name] => Amanco - BR ) ) [1313] => Array ( [Offer] => Array ( [id] => 1313 [name] => Apple - AU ) ) [1315] => Array ( [Offer] => Array ( [id] => 1315 [name] => Apple - FI ) ) [1317] => Array ( [Offer] => Array ( [id] => 1317 [name] => Apple - FR ) ) [104] => Array ( [Offer] => Array ( [id] => 104 [name] => Assinatura Premiada - Editora Abril ) ) [134] => Array ( [Offer] => Array ( [id] => 134 [name] => Assine Sky ) ) [1217] => Array ( [Offer] => Array ( [id] => 1217 [name] => Auto - CO ) ) [1215] => Array ( [Offer] => Array ( [id] => 1215 [name] => Auto - MX ) ) [651] => Array ( [Offer] => Array ( [id] => 651 [name] => Avon Revendedora Virtual - BR ) ) [852] => Array ( [Offer] => Array ( [id] => 852 [name] => Ações que pagam dividendos - BR ) ) [680] => Array ( [Offer] => Array ( [id] => 680 [name] => Babbel CPA - BR ) ) [678] => Array ( [Offer] => Array ( [id] => 678 [name] => Babbel CPL - BR ) ) [565] => Array ( [Offer] => Array ( [id] => 565 [name] => Be2 BR ) ) [448] => Array ( [Offer] => Array ( [id] => 448 [name] => Bebedouros ) ) [402] => Array ( [Offer] => Array ( [id] => 402 [name] => Bem Direto ) ) [80] => Array ( [Offer] => Array ( [id] => 80 [name] => Bem Mais Seguro ) ) [398] => Array ( [Offer] => Array ( [id] => 398 [name] => Bidu ) ) [148] => Array ( [Offer] => Array ( [id] => 148 [name] => Big Prêmio - CPC ) ) [418] => Array ( [Offer] => Array ( [id] => 418 [name] => Bio Ritmo - Free Pass (OLD) ) ) [613] => Array ( [Offer] => Array ( [id] => 613 [name] => Bio Ritmo Free Pass - BR ) ) [756] => Array ( [Offer] => Array ( [id] => 756 [name] => Bio Ritmo Summer Pass - BR ) ) [506] => Array ( [Offer] => Array ( [id] => 506 [name] => Bio Ritmo Winterpass ) ) [182] => Array ( [Offer] => Array ( [id] => 182 [name] => Bom Negocio ) ) [60] => Array ( [Offer] => Array ( [id] => 60 [name] => Bora Viagem ) ) [200] => Array ( [Offer] => Array ( [id] => 200 [name] => Brasil Bingo ) ) [56] => Array ( [Offer] => Array ( [id] => 56 [name] => Brastemp ) ) [826] => Array ( [Offer] => Array ( [id] => 826 [name] => Buscar Ead - BR ) ) [635] => Array ( [Offer] => Array ( [id] => 635 [name] => Candidatos ) ) [728] => Array ( [Offer] => Array ( [id] => 728 [name] => Capital Strategy - IT ) ) [284] => Array ( [Offer] => Array ( [id] => 284 [name] => Cappoia ) ) [1193] => Array ( [Offer] => Array ( [id] => 1193 [name] => Captain Vape ) ) [124] => Array ( [Offer] => Array ( [id] => 124 [name] => Carro dos Sonhos ) ) [569] => Array ( [Offer] => Array ( [id] => 569 [name] => Carsystem - BR ) ) [376] => Array ( [Offer] => Array ( [id] => 376 [name] => CarSystem - Parceiro ) )


Can somewhone help me? I need to put this multidimensional array in a loop. To place in a select or table.

Thanks!

Answer

I assume the result of the code you've given is the value of $apiResponse['response']['data']. So the loop will be:

foreach ($apiResponse['response']['data'] as $data) {
   echo $data['Offer']['id'] . ", " . $data['Offer']['name'];
}

Also a small recommendation for debugging this kind of errors on a non-productive server - add the error_reporting directive to the beginning of your script (or in your bootstrap file):

ini_set('display_errors', 1);
error_reporting(E_ALL);

This way any minor mistakes like accessing array key that doesn't exist will be immediately seen.