php数组输出“null”值(从json文件中提取的值)

时间:2017-03-27 23:46:41

标签: php json

我修改此代码以从json文件而不是xml文件(我的旧代码)获取数据

<?php 
$tracking_id = 'MyID'; //This is used to track the user doing the offer. can be email, clickid, subid.. etc 
$userip = $_SERVER['REMOTE_ADDR']; //We need to get the users ip, so the rss feed can display the correct offers for their country. 
$user_agent = $_SERVER['HTTP_USER_AGENT']; //lets collect their user agent to pass along. 
$max_offers = 5; //max number of offers to display. 


$str = file_get_contents('https://www.cpalead.com/dashboard/reports/campaign_json_load_offers.php?id=296213&geoip='.$userip.'&ua='.urlencode($user_agent).'&subid='.urlencode($tracking_id));
$json = json_decode($str, true);
$opOffer = array();
$offer_cnt = 0; 
foreach($json['offers'] as  $offeritem)
{
$opOffer[$offer_cnt] = array("title" => array($offeritem->title), "link" => array($offeritem->link)); 
$offer_cnt++; 
};
if (0 == count($opOffer)): 
echo 'Sorry there are no offers available for your region at this time.'; 
else: 
echo json_encode($opOffer); 
endif; 


?>

它的输出如下:

[{"title":[null],"link":[null]},{"title":[null],"link":[null]},{"title":[null],"link":[null]},{"title":[null],"link":[null]},{"title":[null],"link":[null]},{"title":[null],"link":[null]},{"title":[null],"link":[null]},{"title":[null],"link":[null]},{"title":[null],"link":[null]},{"title":[null],"link":[null]},{"title":[null],"link":[null]}]

它应该是这样的:

[{"title":[{"What is Your Favorite Time for McDonald's?"}],"link":[{"https:\/\/filetrkr.com\/show.php?l=0&u=31802&id=5882&tracking_id=MyId"}]}]

链接以查看json输出:https://www.cpalead.com/dashboard/reports/campaign_json_load_offers.php?id=296213

3 个答案:

答案 0 :(得分:0)

对于这一行:

$opOffer[$offer_cnt] = array("title" => array($offeritem->title), "link" => array($offeritem->link));

不应该是:

$opOffer[$offer_cnt] = array("title" => $offeritem->title, "link" => $offeritem->link);

或:

$opOffer[$offer_cnt] = array("title" => $offeritem['title'], "link" => $offeritem['link']);

此外,就在这一行之后:

$json = json_decode($str, true);

添加此行并发布结果:

echo "<p>JSON: <pre>".print_r($json,true)."</pre></p>";

最后一点要验证您是否收到了您认为应该获得的JSON。

答案 1 :(得分:0)

我刚改变了:

$opOffer[$offer_cnt] = array("title" => array($offeritem->title), "link" => array($offeritem->link)); 

为:

$opOffer[$offer_cnt] = array("title" => array($offeritem["title"]), "link" => array($offeritem["link"])); 

现在在JSON上给我你想要的东西:

[{"title":["Amplificador de Bater\u00eda"],"link":["http:\/\/gtrcking.com\/offer.php?id=744798&pub=296213&subid=MyID"]},{"title":["\u0645\u0628\u0627\u0631\u0627\u0629 \u0645\u0630\u0647\u0644\u0629!"],"link":["http:\/\/gtrcking.com\/offer.php?id=550635&pub=296213&subid=MyID"]},....

答案 2 :(得分:0)

继承人你想要什么。放手一搏。

var adjustment;
$(function () {
    $("#container1, #container2").sortable({
        group: 'ol.simple_with_animation',
        pullPlaceholder: false,

        onDragStart: function ($item, container, _super) {
            var offset = $item.offset(),
                pointer = container.rootGroup.pointer;

            adjustment = {
                left: pointer.left - offset.left,
                top: pointer.top - offset.top
            };

            _super($item, container);

        },
        onDrag: function ($item, position) {
            $item.css({
                left: position.left - adjustment.left,
                top: position.top - adjustment.top
            });
          //  alert('oink');
        },

        onDrop: function ($item, container, _super) {
            var $clonedItem = $('<li/>').css({ height: 0 });
            $item.before($clonedItem);
            $clonedItem.animate({ 'height': $item.height() });
          //  alert('wew');
            $item.animate($clonedItem.position(), function () {
                $clonedItem.detach();
                _super($item, container);
           //     alert('oink');
            });
         //   alert('oink');
        },

    });
    //alert('oink');
});