【问题标题】:Illegal string offset in PHP multidimensional arrayPHP多维数组中的非法字符串偏移
【发布时间】:2016-06-29 16:11:05
【问题描述】:

抱歉,几乎是“重新发布”问题。几天前,我遇到了一个多维数组 (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 ) 

【问题讨论】:

    标签: php arrays multidimensional-array


    【解决方案1】:

    这段代码

    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)

    【讨论】:

    • 不,我以前试过。仍然得到 Illegal string offset 'Offer' 和 Illegal string offset 'name'
    • 抱歉,它必须是 $apiResponse['response']['data']['data']。查看更新的代码
    • 太棒了!非常感谢!!
    • 我很高兴能帮上忙。快乐编码:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-25
    相关资源
    最近更新 更多