2016-06-29 10 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']の更新は、私は前に試してみました。それでも無効な文字列オフセット「オファー」と不正な文字列オフセット「名前」 –

+0

申し訳ありませんが、$ apiResponse ['response'] ['data'] ['data']である必要があります。更新されたコードを参照 –

+0

素晴らしい!どうもありがとうございました!! –