PHP多维数组中的非法字符串偏移量

时间:2016-06-29 16:11:05

标签: php arrays multidimensional-array

很抱歉几乎" repost"题。几天前我遇到了多维数组(PHP multidimensional array x HasOffers)的问题,现在我从另一个数组访问信息时遇到了麻烦。

我正在调用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 ) 

1 个答案:

答案 0 :(得分:0)

此代码

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

假设子元素中的每个元素都具有Offer属性,而该属性又具有conversions属性

看看问题所在的前几个要素

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

我认为您打算访问$data['Offer']['name']而不是$data['Offer']['conversions']

在您的循环中更新为foreach ($apiResponse['response']['data']['data'] as $data)