php array ouput "null" values (values extracted from json file) - php

I've modify this code to get data from a json file instead of xml file (my old code)
<?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;
?>
and it's output like this:
[{"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]}]
it should be like this:
[{"title":[{"What is Your Favorite Time for McDonald's?"}],"link":[{"https:\/\/filetrkr.com\/show.php?l=0&u=31802&id=5882&tracking_id=MyId"}]}]
Link to see json output: https://www.cpalead.com/dashboard/reports/campaign_json_load_offers.php?id=296213

For the line:
$opOffer[$offer_cnt] = array("title" => array($offeritem->title), "link" => array($offeritem->link));
Shouldn't it be either:
$opOffer[$offer_cnt] = array("title" => $offeritem->title, "link" => $offeritem->link);
or:
$opOffer[$offer_cnt] = array("title" => $offeritem['title'], "link" => $offeritem['link']);
Also, just after the line:
$json = json_decode($str, true);
Add this line and post the results:
echo "<p>JSON: <pre>".print_r($json,true)."</pre></p>";
That last bit to verify you are receiving the JSON you think you should be getting.

I just changed:
$opOffer[$offer_cnt] = array("title" => array($offeritem->title), "link" => array($offeritem->link));
to:
$opOffer[$offer_cnt] = array("title" => array($offeritem["title"]), "link" => array($offeritem["link"]));
And now give me what you want on the 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"]},....

heres what you want. give that a go.
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;
};

Related

How insert content of TXT file to JSON file

I want import the "links" from a links.txt file and put it on a JSON file but the result is like:
{"domain":[ "www.google.es","www.yahoo.com","www.example.com"],
"id":6
},
{"domain":["www.google.es","www.yahoo.com","www.example.com"],
"id":6
},
{"domain":["www.google.es","www.yahoo.com","www.example.com"],
"id":6
}
Expected:
{"domain":"www.google.es","id":6},
{"domain":"www.yahoo.com"],"id":7},
{"domain":["www.example.com"],"id":8}
As you can see, the id is the same at I want differents id and the links are added 3 times the 3 links and I want add 1 link on 1 option.
<?php
$jsonContents = file_get_contents('data/data.json');
$data = json_decode($jsonContents, true);
$fp = 'links.txt';
$last_item = end($data);
$last_item_id = $last_item['id'];
$contents_arr = file($fp,FILE_IGNORE_NEW_LINES);
foreach($contents_arr as $key=>$value) {
$contents_arr[$key] = rtrim($value, "\r");
$data[] = array(
'domain' => $contents_arr,
'id' => $last_item_id+1,
);
}
$json = json_encode($data);
file_put_contents('data/data.json', $json);
$last_item_id+1 returns $last_item_id+1, it does not change $last_item_id variable.
Dont change $contents_arr array used for loop inside the loop.
You use for domain $contents_arr, which contains all values. Create for domain new array, ie:
foreach($contents_arr as $key=>$value) {
$data[] = array(
'domain' => [rtrim($value, "\r")],
'id' => ++$last_item_id,
);
}

How to add random url to a href inside a for loop in php?

I have a scenario :
There are some divs are displaying the based on the following loop:
<?php foreach($posts->content as $entry) { ?>
<div><a class="popup-with-zoom-anim wiplay" id="<?=$entry->id?>"
href="#small-dialog" data-detail-id ="<?=$entry->id?>"
data-stream="******">content here</a></div>
<?php } ?>
In the a href "data-stream" i want to pass some youtube url so the each div will show random url based on th for loop.
I have the following code tried.But not working.How will we call this function inside the loop?
$assoc_array = array( "url" => "https://www.youtube.com
/watch?v=A7XdOyZIkko",
"url" => "https://www.youtube.com/watch?v=dMH0bHeiRNg
",
"url" => "https://www.youtube.com/watch?v=xEs59zTXu7s",
"url" => "https://www.youtube.com/watch?v=tlDAgZO2ZDM
");
function shuffle_assoc_array(&$array) {
if (!is_array($array)) return $array;
$keys = array_keys($array);
shuffle($keys);
$random = array();
foreach ($keys as $key)
$random[$key] = $array[$key];
$array = $random; return TRUE;
}
shuffle_assoc_array($assoc_array);
How can i pass this youtube url randomly to the for loop?
As Ima told, you must have to cahnge your array format and than try with below code.
<?php
$assoc_array = array( "https://www.youtube.com/watch?v=A7XdOyZIkko", "https://www.youtube.com/watch?v=dMH0bHeiRNg", "https://www.youtube.com/watch?v=xEs59zTXu7s", "https://www.youtube.com/watch?v=tlDAgZO2ZDM");
shuffle($assoc_array);
for($i=0;$i<count($assoc_array);$i++){
echo '<div><a class="popup-with-zoom-anim wiplay" id="'.$i.'" href="#small-dialog" data-detail-id ="'.$i.'" data-stream="'.$assoc_array[$i].'">'.$assoc_array[$i].'</a></div>';
}
?>
THis array will give you only one value
array(
"url" => "https://www.youtube.com
/watch?v=A7XdOyZIkko",
"url" => "https://www.youtube.com/watch?v=dMH0bHeiRNg",
"url" => "https://www.youtube.com/watch?v=xEs59zTXu7s",
"url" => "https://www.youtube.com/watch?v=tlDAgZO2ZDM
");
This is equivalent to
array(
"url" => "https://www.youtube.com/watch?v=tlDAgZO2ZDM
");
Try something like this
array(
"https://www.youtube.com/watch?v=A7XdOyZIkko",
"https://www.youtube.com/watch?v=dMH0bHeiRNg",
"https://www.youtube.com/watch?v=xEs59zTXu7s",
"https://www.youtube.com/watch?v=tlDAgZO2ZDM"
);

how to make simple quiz and button answer in telegram bot with php?

I want to make a simple question and the answer in multiple choice, the result what i want is like this picture : this is picture for desired the multiple choice button
but the result is not like what i desired this is the picture for result from my code : this is picture for not desired result
and this is the code that i have tried :
<?php
$token = "##################################";
$website = "https://api.telegram.org/bot".$token;
$array = array(array("1","2","3"));
$btn = array("keyboard" => $array,"hide_keyboard" => true,'selective' => true,);
$reply = json_encode($btn);
$url = $website."/sendmessage?chat_id=$chatIdtext=select : &reply_markup=".$reply;
file_get_contents($url);
?>
try this code, maybe the result like what you want.
<?php
$token = "######################################";
$website = "https://api.telegram.org/bot".$token;
$name1 = 'das';
$name2 = 'dsa';
$placeId1 = '1';
$placeId2 = '2';
$keyboard = [
'inline_keyboard' => [[['text' => $name1, 'callback_data' => $placeId1]], [['text' => $name2, 'callback_data' => $placeId2]]],
];
$reply = json_encode($keyboard);
$url = $website."/sendmessage?chat_id=130434215&text=select : &reply_markup=".$reply;
file_get_contents($url);
?>

PHP - Array inside array dynamically

Previous, I have searched in Google. But I don't find what I want...
I want to add 2 array to an array. This is my code :
$labelCollection = array();
$labelArray1 = array("Staff ID", "Photo Profile");
$labelArray2 = array("Religion", "Postcode");
But, I want to make the result like this :
$labelCollection =
array('info_1' => array('Staff ID', 'Photo Profile'),
'info_2' => array('Religion', 'Postcode')
);
Try
<?php
$labelCollection = array();
$labelArray1 = array("Staff ID", "Photo Profile");
$labelArray2 = array("Religion", "Postcode");
$labelCollection = array('info_1' => $labelArray1 ,
'info_2' => $labelArray2
);

PHP - Random text with two strings

How would I join these two strings and create random link based on the entry list.
// Add a link and the associated image //
$adlink1="http://www.****.com/sale.php";
$adlinkpic1="http://www.***-cdn.com/blogAssets/ad/1.jpg";
$adlink2="http://www.*****.com/sale.php";
$adlinkpic2="http://www.**-cdn.com/blogAssets/ad/2.jpg";
$adlink3="http://www.**.com/product.php?prodref=564_white&ref=AddSphere";
$adlinkpic3="http://www.**-cdn.com/blogAssets/ad/3.jpg";
$adlink4="http://www.**.com/wedding-boutique.php";
$adlinkpic4="http://www.**-cdn.com/blogAssets/ad/4.jpg";
$adlink5="http://www.**.com/made-to-measure-service.php";
$adlinkpic5="http://www.**-cdn.com/blogAssets/ad/5.jpg";
// SHOW ONE AD LINK
srand ((double) microtime() * 1000000);
$adlink[] + $adlinkpic[] = rand(0,count($quotes)-1);
echo "<a href='$adlink'><img src='$adlinkpic' />";
// SHOW TWO AD LINKS /cannot be same
// code here
It is easy to put all of your links in an (associative) array then use array functions to manipulate them:
<?php
$ad = array(
array(
"url" => "http://www.****.com/sale.",
"img" => "http://www.***-cdn.com/blogAssets/ad/1.jpg"
),
array(
"url" => "http://www.*****.com/sale.",
"img" => "http://www.**-cdn.com/blogAssets/ad/2.jpg"
),
array(
"url" => "http://www.**.com/product.php",
"img" => "http://www.**-cdn.com/blogAssets/ad/3.jpg"
),
array(
"url" => "http://www.**.com/wedding-boutique.",
"img" => "http://www.**-cdn.com/blogAssets/ad/4.jpg"
),
array(
"url" => "http://www.**.com/made-to-measure-service.",
"img" => "http://www.**-cdn.com/blogAssets/ad/5.jpg"
)
// more ads
);
$id = array_rand($ad); // choose a random index from the array
echo "<img src=\"{$ad[$id]['img']}\" />\n";
unset($ad[$id]); // remove the chosen one so that it is not displayed on next pass
$id = array_rand($ad);
echo "<img src=\"{$ad[$id]['img']}\" />\n";
unset($ad[$id]);
I'd be tempted to do it something like this:
<?php
// Add a link and the associated image //
$adlink1="http://www.****.com/sale.php";
$adlinkpic1="http://www.***-cdn.com/blogAssets/ad/1.jpg";
$adlink2="http://www.*****.com/sale.php";
$adlinkpic2="http://www.**-cdn.com/blogAssets/ad/2.jpg";
$adlink3="http://www.**.com/product.php?prodref=564_white&ref=AddSphere";
$adlinkpic3="http://www.**-cdn.com/blogAssets/ad/3.jpg";
$adlink4="http://www.**.com/wedding-boutique.php";
$adlinkpic4="http://www.**-cdn.com/blogAssets/ad/4.jpg";
$adlink5="http://www.**.com/made-to-measure-service.php";
$adlinkpic5="http://www.**-cdn.com/blogAssets/ad/5.jpg";
$links = array();
$links[0]=array('link'=>$adlink1,'pic'=>$adlinkpic1);
$links[1]=array('link'=>$adlink2,'pic'=>$adlinkpic2);
$links[2]=array('link'=>$adlink3,'pic'=>$adlinkpic3);
$links[3]=array('link'=>$adlink4,'pic'=>$adlinkpic4);
$links[4]=array('link'=>$adlink5,'pic'=>$adlinkpic5);
$alreadyAdded=array();
for ($i=0;$i<2;$i++) {
$added = false;
while (!$added) {
// generate random number
$rand = mt_rand(0,4);
if (!in_array($rand,$alreadyAdded)) {
echo "<a href='".$links[$rand]['link']."'><img src='".$links[$rand]['pic']."' />";
$added = true;
$alreadyAdded[]=$rand;
}
}
}
Edit: noticed you wanted more than 1 outputted, updated code to reflect.

Categories