2017-03-27 69 views
0

我已經修改這個代碼從一個JSON文件,而不是XML文件(我的舊代碼)PHP數組輸出中「空」的值(值從JSON文件中提取)

<?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

回答

0

對於行:

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

它不應該是兩種:

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

或:

$json = json_decode($str, true); 

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

此外,剛剛行後

加入這一行,並張貼結果:

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

最後一點,以驗證您收到你認爲你應該得到的JSON。

+0

現在的輸出的確切格式使用該: $ opOffer [$ offer_cnt] =陣列( 「標題」=> $ offeritem [ '標題'], 「鏈接」=> $ offeritem ['連結「]); 只需要5個優惠($ max_offers = 5;)並非所有優惠 – HASH717

+0

因此,在foreach中,測試max記錄並跳出循環:if(cnt> 5){break; } –

+0

是它的工作,但輸出showup這樣的: [{},{},{}] 它應該是這樣的: [{[{}],[{}],[{}]} ] – HASH717

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"]},.... 
+0

是的,但如何最大提供只有5? – HASH717

0

繼承人你想要什麼。放棄一下。

foreach($json['offers'] as $offeritem) 
{ 
$opOffer[$offer_cnt] = array("title" => array($offeritem['title']), "link" => array($offeritem['link'])); 
$offer_cnt++; 
if ($offer_cnt == $max_offers) break; 
};