2016-06-29 4 views
-2

죄송합니다. 며칠 전 다차원 배열 (PHP multidimensional array x HasOffers)에 문제가있어 다른 배열에서 정보에 액세스하는 데 문제가 있습니다.PHP 다차원 배열의 문자열 오프셋이 잘못되었습니다.

API 호출을 사용하여 데이터베이스에서 일부 데이터를 내보내는 중입니다. 나는 일부 필드를 수출하고 다차원 배열을 볼 수 있지만, 내가 루프에 넣어 때이 오류 얻을 :

Warning: Illegal string offset 'Offer' in /home/track374/public_html/the-ybox.tech/getStats_table.php on line 71 
Warning: Illegal string offset 'conversions' in /home/track374/public_html/the-ybox.tech/getStats_table.php on line 71 
Warning: Illegal string offset 'Stat' in /home/track374/public_html/the-ybox.tech/getStats_table.php on line 71 
Warning: Illegal string offset 'conversions' in /home/track374/public_html/the-ybox.tech/getStats_table.php on line 71 

이것은 루프를 사용하여 API 호출 코드 :

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

    // Specify method arguments 
    $args = array(
     'NetworkId' => 'xxxxxxxxxx', 
     'Target' => 'Report', 
     'Method' => 'getStats', 
     'NetworkToken' => 'xxxxxxxxxx', 
     'fields' => array(
      'Stat.conversions', 
      'Offer.name' 
     ), 
     'groups' => array(
      'Stat.affiliate_id' 
     ), 
     'page' => '3', 
     'data_start' => '2016-06-28', 
     'data_end' => '2016-06-29' 
    ); 

    // 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; 
     echo "<table border='1' width='100%'>"; 
     echo "<tr>"; 
     echo "<td bgcolor='#ccc'>ID</td><td bgcolor='#ccc'>Nome da campanha</td>"; 
     echo "</tr>"; 

     // Loop through Array 
     foreach ($apiResponse['response']['data'] as $data) { 
      echo "<tr><td>" . $data['Offer']['conversions'] . "</td><td>" . utf8_decode($data['Stat']['conversions']) . "</td></tr>"; 
     } 

     echo "</table>"; 
    } 
    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; 
    } 
?> 

가 그리고 이것은이다 다차원 결과 :

Array ([page] => 3 [current] => 50 [count] => 119 [pageCount] => 3 [data] => Array ([0] => Array ([Stat] => Array ([conversions] => 32 [affiliate_id] => 1291) [Offer] => Array ([name] => Maria - BR)) [1] => Array ([Stat] => Array ([conversions] => 34 [affiliate_id] => 1000) [Offer] => Array ([name] => Englishtown - BR)) [2] => Array ([Stat] => Array ([conversions] => 38 [affiliate_id] => 1056) [Offer] => Array ([name] => Englishtown - BR)) [3] => Array ([Stat] => Array ([conversions] => 39 [affiliate_id] => 1052) [Offer] => Array ([name] => Englishtown - BR)) [4] => Array ([Stat] => Array ([conversions] => 53 [affiliate_id] => 1228) [Offer] => Array ([name] => Natura Consultora CND - BR)) [5] => Array ([Stat] => Array ([conversions] => 53 [affiliate_id] => 1513) [Offer] => Array ([name] => Englishtown - BR)) [6] => Array ([Stat] => Array ([conversions] => 53 [affiliate_id] => 1456) [Offer] => Array ([name] => Tara - MX)) [7] => Array ([Stat] => Array ([conversions] => 58 [affiliate_id] => 1465) [Offer] => Array ([name] => Vsepo49 - RU)) [8] => Array ([Stat] => Array ([conversions] => 60 [affiliate_id] => 1539) [Offer] => Array ([name] => Trilhonario - BR)) [9] => Array ([Stat] => Array ([conversions] => 71 [affiliate_id] => 1130) [Offer] => Array ([name] => Englishtown - BR)) [10] => Array ([Stat] => Array ([conversions] => 90 [affiliate_id] => 1505) [Offer] => Array ([name] => Englishtown - BR)) [11] => Array ([Stat] => Array ([conversions] => 107 [affiliate_id] => 1318) [Offer] => Array ([name] => Proteste - BR)) [12] => Array ([Stat] => Array ([conversions] => 121 [affiliate_id] => 1404) [Offer] => Array ([name] => Trilhonario - MX)) [13] => Array ([Stat] => Array ([conversions] => 237 [affiliate_id] => 1040) [Offer] => Array ([name] => Englishtown - BR)) [14] => Array ([Stat] => Array ([conversions] => 268 [affiliate_id] => 1064) [Offer] => Array ([name] => Turma da Mônica - BR)) [15] => Array ([Stat] => Array ([conversions] => 350 [affiliate_id] => 1248) [Offer] => Array ([name] => Proteste - BR)) [16] => Array ([Stat] => Array ([conversions] => 540 [affiliate_id] => 1046) [Offer] => Array ([name] => Trilhonario - BR)) [17] => Array ([Stat] => Array ([conversions] => 720 [affiliate_id] => 1066) [Offer] => Array ([name] => Englishtown - BR)) [18] => Array ([Stat] => Array ([conversions] => 823 [affiliate_id] => 1208) [Offer] => Array ([name] => Proteste - BR))) [dbSource] => branddb) 

답변

0

이 코드

foreach ($apiResponse['response']['data'] as $data) { 
    echo "<tr><td>" . $data['Offer']['conversions'] . "</td><td>" . utf8_decode($data['Stat']['conversions']) . "</td></tr>"; 
} 

는 자식 요소의 모든 요소가 다시 문제가

[Offer] => Array ([name] => Maria - BR) ... 

입니다 그것은 분명 처음 몇 요소에서 찾고 conversions 재산

이있는 Offer 재산권 난 당신이 액세스 의미 생각이 있다고 가정합니다 foreach ($apiResponse['response']['data']['data'] as $data)

+0

아니 당신의 루프에서 $data['Offer']['name'] 대신

$data['Offer']['conversions']의 업데이트는, 내가 이전에 시도했습니다. 여전히 잘못된 문자열 오프셋 'Offer'및 잘못된 문자열 오프셋 'name' –

+0

$ apiResponse [ 'response'] [ 'data'] [ 'data'] 죄송합니다. 업데이트 된 코드보기 –

+0

멋지다! 대단히 감사합니다 !! –

관련 문제