The script below apparently uses the API documented at http://www.hasoffers.com/wiki/Offer:create. S the question has (at least) two parts: a) How to store more than one data set within an array. b) Does the API accept it....
When i run the script it only stores the last value inside 'data' how can i get it to store more data at once?
The code below has for example 2 values. one is caled LOLO and the other one is caled LELE.
The output shows only the value LELE.
this is the code.
<?php
header('Content-type: application/json');
$base = 'http://api.hasoffers.com/Api?';
$params = array(
'Format' => 'json'
,'Target' => 'Offer'
,'Method' => 'create'
,'Service' => 'HasOffers'
,'Version' => 2
,'NetworkId' => 'demo'
,'NetworkToken' => '....'
,'data' => array(
'name' => 'LOLO'
,'description' => 'test'
,'offer_url' => 'http://google.nl'
,'preview_url' => 'http://google.nl'
,'expiration_date' => '08-08-2013'
,'name' => 'LELE'
,'description' => 'test'
,'offer_url' => 'http://google.nl'
,'preview_url' => 'http://google.nl'
,'expiration_date' => '08-08-2013'
)
);
$url = $base . http_build_query( $params );
$result = file_get_contents( $url );
print_r( json_decode( $result) );
?>
and this is the output
[request] => stdClass Object
(
[Target] => Offer
[Format] => json
[Service] => HasOffers
[Version] => 2
[Method] => create
[NetworkId] => demo
[NetworkToken] => NETU2nzMw8AYS6EGgjFrjGR88GcSiF
[data] => stdClass Object
(
[name] => LELE
[description] => test
[offer_url] => http://google.nl
[preview_url] => http://google.nl
[expiration_date] => 08-08-2013
)
)
'data' => array(
array (
'name' => 'LOLO',
'description' => 'test',
'offer_url' => 'http://google.nl',
'preview_url' => 'http://google.nl'
'expiration_date' => '08-08-2013'),
array (
'name' => 'LELE',
'description' => 'test',
'offer_url' => 'http://google.nl',
'preview_url' => 'http://google.nl',
'expiration_date' => '08-08-2013'))
,'data' => array( array(
'name' => 'LOLO'
,'description' => 'test'
,'offer_url' => 'http://google.nl'
,'preview_url' => 'http://google.nl'
,'expiration_date' => '08-08-2013'
),
array(
,'name' => 'LELE'
,'description' => 'test'
,'offer_url' => 'http://google.nl'
,'preview_url' => 'http://google.nl'
,'expiration_date' => '08-08-2013'
)
)
You need to add it as a multi dimensional array or else it will overwrite the elements with same key. Please note the array (....) addd inside your array
array( array(
Or you can store it in this way
$data = array();
$data[0] = array('name' => 'LELE'
, 'description' => 'test'
, 'offer_url' => 'http://google.nl'
, 'preview_url' => 'http://google.nl'
, 'expiration_date' => '08-08-2013'
);
$data[1] = array('name' => 'LOLO'
, 'description' => 'test'
, 'offer_url' => 'http://google.nl'
, 'preview_url' => 'http://google.nl'
, 'expiration_date' => '08-08-2013'
);
$params = array(
'Format' => 'json'
, 'Target' => 'Offer'
, 'Method' => 'create'
, 'Service' => 'HasOffers'
, 'Version' => 2
, 'NetworkId' => 'demo'
, 'NetworkToken' => 'NETU2nzMw8AYS6EGgjFrjGR88GcSiF'
, 'data' => $data
);
print_r($params);
// Output
Array
(
[Format] => json
[Target] => Offer
[Method] => create
[Service] => HasOffers
[Version] => 2
[NetworkId] => demo
[NetworkToken] => NETU2nzMw8AYS6EGgjFrjGR88GcSiF
[data] => Array
(
[0] => Array
(
[name] => LELE
[description] => test
[offer_url] => http://google.nl
[preview_url] => http://google.nl
[expiration_date] => 08-08-2013
)
[1] => Array
(
[name] => LOLO
[description] => test
[offer_url] => http://google.nl
[preview_url] => http://google.nl
[expiration_date] => 08-08-2013
)
)
)
a) How to store more than one data set within an array.
$fixed_params = array(
'Format' => 'json'
,'Target' => 'Offer'
,'Method' => 'create'
,'Service' => 'HasOffers'
,'Version' => 2
,'NetworkId' => 'demo'
,'NetworkToken' => '....'
);
$offer_data = array(
array(
'name' => 'LOLO'
,'description' => 'test'
,'offer_url' => 'http://google.nl'
,'preview_url' => 'http://google.nl'
,'expiration_date' => '08-08-2013'
),
array(
'name' => 'LELE'
,'description' => 'test'
,'offer_url' => 'http://google.nl'
,'preview_url' => 'http://google.nl'
,'expiration_date' => '08-08-2013'
)
);
// store all results here
$result = array();
// iterates two times since $offer_data has two elements.
foraech ($offer_data as $offern => $data ){
// store offer's data into fixed_params['data'] element.
$fixed_params['data'] = $data;
$url = $base . http_build_query( $fixed_params );
$result[] = json_decode( file_get_contents( $url ));
}
print_r($result);
b) Does the API accept it....
As I understand the API, it accepts only one create at time. See http://www.hasoffers.com/wiki/Offer:create it says: Creates a new offer.
This is what i did and it works
<?php
// Bestand openen
if (($file = fopen("test2.csv", "r")) !== FALSE) {
// Eerste rij van Excel als value gebruiken
$header = fgetcsv($file, 1000, ";");
// Een loop door Excel file
while (($data = fgetcsv($file, 1000, ";")) !== FALSE) {
// combineer de eerste rij met de gegevens
$combined = array_combine($header,$data);
// Connectie maken met Hasoffers bij elke waarden
$base = 'http://api.hasoffers.com/Api?';
$params = array(
'Format' => 'json'
,'Target' => 'Offer'
,'Method' => 'create'
,'Service' => 'HasOffers'
,'Version' => 2
,'NetworkId' => 'demo'
,'NetworkToken' => '.....'
,'data' => $combined
);
$url = $base . http_build_query( $params );
$result = file_get_contents( $url );
// Tijdelijk printen
print_r( json_encode( $result) );
}
}
?>
I've created a loop including a CSV file.
the problem was that it connected only once with hasoffer and that allowed only one value.
Related
I'm making a notification system (Let me know when you arrive) and I'm having trouble grouping by email. I need to group the products with specific emails to set up email sending.My problem is to separate the separate output by email
Array
$resultado = array(
array( 'email' => 'de#email.com' , 'ean' => '1278', 'desc' => 'PROD1' ),
array( 'email' => 'de#email.com' , 'ean' => '2342', 'desc' => 'PROD2' ),
array( 'email' => 'de#email.com' , 'ean' => '7567', 'desc' => 'PROD3' ),
array( 'email' => 'ma#email.com' , 'ean' => '5555', 'desc' => 'PROD4' ),
array( 'email' => 'ma#email.com' , 'ean' => '3335', 'desc' => 'PROD5' ),
);
Group
$saida = array();
foreach ($resultado as $res) {
if( !isset( $saida[$res['email']] ) ) {
$saida[$res['email']] = array();
}
array_push( $saida[$res['email']], $res['ean'], $res['desc'], $res['email']);
}
OUT
echo"<pre>";
print_r( $saida );
echo"</pre>";
Array
(
[de#email.com] => Array
(
[0] => 1278
[1] => PROD1
[2] => de#email.com
[3] => 2342
[4] => PROD2
[5] => de#email.com
[6] => 7567
[7] => PROD3
[8] => de#email.com
)
[ma#email.com] => Array
(
[0] => 5555
[1] => PROD4
[2] => ma#email.com
[3] => 3335
[4] => PROD5
[5] => ma#email.com
)
)
I NEED DISPLAY A BREAK LINE (Only when the email is different)
PROD1 - 1278 - de#email.com
PROD2 - 2342 - de#email.com
PROD3 - 7567 - de#email.com
___________________________________
PROD4 - 5555 - ma#email.com
PROD5 - 3335 - ma#email.com
And send emails
$return = sendEmail( TO, SITE, '$mails_HERE' , HOST, '[' . SITE . '] ' . $subject, $content);
Here you can first filter out the unique mails and then search the array for corresponding emails data.
$resultado = array(
array( 'email' => 'de#email.com' , 'ean' => '1278', 'desc' => 'PROD1' ),
array( 'email' => 'de#email.com' , 'ean' => '2342', 'desc' => 'PROD2' ),
array( 'email' => 'de#email.com' , 'ean' => '7567', 'desc' => 'PROD3' ),
array( 'email' => 'ma#email.com' , 'ean' => '5555', 'desc' => 'PROD4' ),
array( 'email' => 'ma#email.com' , 'ean' => '3335', 'desc' => 'PROD5' ),
);
$emails = array_unique(array_column( $resultado, 'email' ));
$grouped = [];
//looking for each email's corresponding data and saving it in group array
foreach ($emails as $key1 => $each ) {
foreach ($resultado as $value) {
if( $value['email'] === $each ){
unset($value['email']);
$grouped[$each][] = $value;
}
}
}
// printing the group data
foreach ($grouped as $key => $each_member ) {
foreach ($each_member as $value) {
echo"<pre>";
echo $value['desc'] . ' ' . $value['ean'] . ' ' . $key;
echo"</pre>";
}
echo '-------------------------';
}
you just need to group your items.
$results = array(
array( 'email' => 'de#email.com' , 'ean' => '1278', 'desc' => 'PROD1' ),
array( 'email' => 'de#email.com' , 'ean' => '2342', 'desc' => 'PROD2' ),
array( 'email' => 'de#email.com' , 'ean' => '7567', 'desc' => 'PROD3' ),
array( 'email' => 'ma#email.com' , 'ean' => '5555', 'desc' => 'PROD4' ),
array( 'email' => 'ma#email.com' , 'ean' => '3335', 'desc' => 'PROD5' )
);
$data = [];
foreach ($results as $result) {
$data[$result['email']][] = $result;
}
And that's it.
actually i am tring to get data from url which i already got but i can't able to insert data into the array please check values are dummy1 dummy2
Array ( [id] => 491 [headings] => ‘సౌందర్య లహరి’ ఎలా ఉంది అంటే..? [content] => శ్రీ వాసు దర్శకత్వం లో బెల... [userPic] => 682473.jpg ) JSON sent: {"app_id":"35fi6c27-5f39-4s9f-94db-79bf123g0f9","included_segments":["All"],"data":{"foo":"bar"},"contents":{"en":"dummy1"},"headings":{"en":"dummy2"},"url":"http:\/\/www.gggg.in\/view.php?id=[id]","chrome_web_image":"http:\/\/www.ggg.in\/admin\/user_images\/[userPic]"} JSON received: {"allresponses":"{\"id\":\"f66a03a4-1b17-8tf3-93ed-f3ad6rt7cdb9\",\"recipients\":6}"}
the above data got from url
http://www.ggg.in/gistfile1.php?id=491&headings=%E2%80%98%E0%B0%B8%E0%B1%8C%E0%B0%82%E0%B0%A6%E0%B0%B0%E0%B1%8D%E0%B0%AF%20%E0%B0%B2%E0%B0%B9%E0%B0%B0%E0%B0%BF%E2%80%99%20%E0%B0%8E%E0%B0%B2%E0%B0%BE%20%E0%B0%89%E0%B0%82%E0%B0%A6%E0%B0%BF%20%E0%B0%85%E0%B0%82%E0%B0%9F%E0%B1%87..?&content=%20%E0%B0%B6%E0%B1%8D%E0%B0%B0%E0%B1%80%20%E0%B0%B5%E0%B0%BE%E0%B0%B8%E0%B1%81%20%E0%B0%A6%E0%B0%B0%E0%B1%8D%E0%B0%B6%E0%B0%95%E0%B0%A4%E0%B1%8D%E0%B0%B5%E0%B0%82%20%E0%B0%B2%E0%B1%8B%20%E0%B0%AC%E0%B1%86%E0%B0%B2...&userPic=682473.jpg
and i need to insert the values in this function
function sendMessage() {
$content = array(
"en" => 'dummy1'
);
$headings = array(
"en" => 'dummy2'
);
$hashes_array = array();
$fields = array(
'app_id' => "35fi6c27-5f39-4s9f-94db-79bf123g0f9",
'included_segments' => array(
'All'
),
'data' => array(
"foo" => "bar"
),
'contents' => $content,
'headings' => $headings,
'url' => 'http://www.gggg.in/view.php?id=[id]',
'chrome_web_image' => 'http://www.ggg.in/admin/user_images/[userPic]',
);
OneSignal does not support substituting variable data directly into API calls. However, you can achieve this with Tag and Variable Substitution: https://documentation.onesignal.com/docs/personalization
this is the solutions for this Question i asked about
actually i have a varable passing throug url and i need them to insert into function
$array = [
'id' => 498,
'value2' => 'యూట్యూబ్లో “భరత్ అనే నేను” అన్ కట్ సీన్లు..!',
'value1' => 'కొరటాల శివ దర్శకత్వం లో సూ...',
'value3' => '525722.jpg'
];
sendMessage($array);
function sendMessage($array) {
$content = array(
"en" => $array['value1']
);
$headings = array(
"en" => $array['value2']
);
$hashes_array = array();
$fields = array(
'app_id' => "31pe2347-5i39-4y9f-2222-79b5875f00f9",
'included_segments' => array(
'All'
),
'data' => array(
"foo" => "bar"
),
'contents' => $content,
'headings' => $headings,
'url' => 'http://w...content-available-to-author-only...9.in/view.php?id=' . $array['id'],
'chrome_web_image' => 'http://w...content-available-to-author-only...9.in/admin/user_images/' . $array['value3'],
);
print_r($fields);
}
I'm storing data to an array like this which is inside three nested loops (loops omitted):
$teamDetails[$k] = array(
'side' => $json['data'][$i]['rosters'][$k]['side'],
'gold' => $json['data'][$i]['rosters'][$k]['gold'],
'aces' => $json['data'][$i]['rosters'][$k]['aces_earned'],
'herokills' => $json['data'][$i]['rosters'][$k]['hero_kills'],
'winner' => translateGame($json['data'][$i]['rosters'][$k]['winner']),
'participants'[$j] => array(
'work' => 'it worked',
)
);
How can make 'participants' an array with the indices coming from $j?
That's easy
$teamDetails[$k] = array(
'side' => $json['data'][$i]['rosters'][$k]['side'],
'gold' => $json['data'][$i]['rosters'][$k]['gold'],
'aces' => $json['data'][$i]['rosters'][$k]['aces_earned'],
'herokills' => $json['data'][$i]['rosters'][$k]['hero_kills'],
'winner' => translateGame($json['data'][$i]['rosters'][$k]['winner']),
'participants' => array(
$j => array(
'work' => 'it worked',
))
);
I've been trying to organize data into a multidimensional array from a foreach loop but the data is all over the place.
This is what I coded:
$productIDs = array(
'0' => array(
'product_id' => '10',
'product_name' => 'Test',
'product_file' => 'file10',
'product_image' => 'https://i.imgur.com/dXb6.png',
),
'1' => array(
'product_id' => '20',
'product_name' => 'Test1',
'product_file' => 'file20',
'product_image' => 'https://i.imgur.com/MuP8.png',
),
'2' => array(
'product_id' => '30',
'product_name' => 'No product',
'product_file' => 'file30',
'product_image' => 'https://i.imgur.com/kWP3.png',
)
);
$urlIDs = array(10,20);
function getFiles($productIDs, $urlIDs)
{
foreach($productIDs as $ids)
{
foreach($urlIDs as $products)
{
if(in_array($products, $ids)){
$data1[] = $ids['product_id'];
$data2[] = $ids['product_file'];
$data3[] = $ids['product_image'];
}
}
}
return array($data1, $data2, $data3);
}
$getFiles = getFiles($productIDs, $urlIDs);
And the output for the following is:
Array
(
[0] => Array
(
[0] => 10
[1] => 20
)
[1] => Array
(
[0] => file10
[1] => file20
)
[2] => Array
(
[0] => https://i.imgur.com/dXb6.png
[1] => https://i.imgur.com/MuP8.png
)
)
Although what I'm trying to accomplish is:
Array
(
[0] => Array
(
[id] => 10
[file] => file10
[image] => https://i.imgur.com/dXb6.png
)
[1] => Array
(
[id] => 20
[file] => file20
[image] => https://i.imgur.com/MuP8.png
)
)
I tried the following:
function getFiles($productIDs, $urlIDs)
{
foreach($productIDs as $ids)
{
foreach($urlIDs as $products)
{
if(in_array($products, $ids)){
$data['id'] = $ids['product_id'];
$data['file'] = $ids['product_file'];
$data['image'] = $ids['product_image'];
}
}
}
return array($data);
}
Which returns the following without looping through all id's, it should return both arrays since both id's are matching.
Array
(
[0] => Array
(
[id] => 20
[file] => file20
[image] => https://i.imgur.com/MuP8.png
)
)
You could say the first batch of code without the specific key names isn't what I want, but I just wanted to show what I had tried. The last batch works (with key names), but doesn't loop through all of the $urlIDs, and for some reason that I can't understand, the code isn't even returning id = 10, it's returning id = 20, though it's second in the array. If someone could explain why this is happening I'd appreciate it.
In case it's useful: https://eval.in/764083
PHP code demo
<?php
$productIDs = array(
'0' => array(
'product_id' => '10',
'product_name' => 'Test',
'product_file' => 'file10',
'product_image' => 'https://i.imgur.com/dXb6.png',
),
'1' => array(
'product_id' => '20',
'product_name' => 'Test1',
'product_file' => 'file20',
'product_image' => 'https://i.imgur.com/MuP8.png',
),
'2' => array(
'product_id' => '30',
'product_name' => 'No product',
'product_file' => 'file30',
'product_image' => 'https://i.imgur.com/kWP3.png',
)
);
$urlIDs = array(10,20);
function getFiles($productIDs, $urlIDs)
{
$result=array();
foreach($productIDs as $key => $productData)
{
if(!in_array($productData['product_id'],$urlIDs))
{
unset($productIDs[$key]);
}
else
{
$result[]=array("id"=>$productData['product_id'],"file"=>$productData['product_file'],"image"=>$productData['product_image']);
}
}
return $result;
}
$getFiles = getFiles($productIDs, $urlIDs);
print_r($getFiles);
Output:
Array
(
[0] => Array
(
[id] => 10
[file] => file10
[image] => https://i.imgur.com/dXb6.png
)
[1] => Array
(
[id] => 20
[file] => file20
[image] => https://i.imgur.com/MuP8.png
)
)
Leveraging your tried out solution, change your tried solution to this.
function getFiles($productIDs, $urlIDs)
{
foreach($productIDs as $ids)
{
foreach($urlIDs as $products)
{
if(in_array($products, $ids)){
$data[] = [ "id" => $ids['product_id'],
"file" => $ids['product_file'],
"image" => $ids['product_image']];
}
}
}
return $data;
}
Try this one.
function getFiles($productIDs, $urlIDs)
{
$newDara = [];
foreach($productIDs as $ids)
{
foreach($urlIDs as $products)
{
if(in_array($products, $ids)){
$data['id'] = $ids['product_id'];
$data['file'] = $ids['product_file'];
$data['image'] = $ids['product_image'];
$newData[] = $data;
}
}
}
return array($newData);
}
Try this version
<?php
/**
* Created by PhpStorm.
* User: lenovo
* Date: 3/30/2017
* Time: 5:53 AM
*/
$productIDs = array(
'0' => array(
'product_id' => '10',
'product_name' => 'Test',
'product_file' => 'file10',
'product_image' => 'https://i.imgur.com/dXb6.png',
),
'1' => array(
'product_id' => '20',
'product_name' => 'Test1',
'product_file' => 'file20',
'product_image' => 'https://i.imgur.com/MuP8.png',
),
'2' => array(
'product_id' => '30',
'product_name' => 'No product',
'product_file' => 'file30',
'product_image' => 'https://i.imgur.com/kWP3.png',
)
);
$urlIDs = array(10,20);
$ouput = [];
foreach($productIDs as $product){
if(in_array($product['product_id'],$urlIDs)){
$ouput[] = array("id"=>$product["product_id"], "file"=>$product["product_file"], "image"=>$product["product_image"]);
}
}
echo "<pre>";
print_r($ouput);
echo "</pre>";
Check the
Good Luck
The other answers are either doing too many loops, or looping through the $productIDs array (which I assume is much longer in your actual project).
This method will only iterate on your search array, this should provide a performance boost. You will see this approach posted as the top comment # http://php.net/manual/en/function.array-search.php
Code: (Demo)
$productIDs = array(
'0' => array(
'product_id' => '10',
'product_name' => 'Test',
'product_file' => 'file10',
'product_image' => 'https://i.imgur.com/dXb6.png',
),
'1' => array(
'product_id' => '20',
'product_name' => 'Test1',
'product_file' => 'file20',
'product_image' => 'https://i.imgur.com/MuP8.png',
),
'2' => array(
'product_id' => '30',
'product_name' => 'No product',
'product_file' => 'file30',
'product_image' => 'https://i.imgur.com/kWP3.png',
)
);
$urlIDs=array(10,20);
foreach($urlIDs as $prd_id){
if(($i=array_search($prd_id,array_column($productIDs,'product_id')))!==false){
$result[]=['id'=>$productIDs[$i]['product_id'],
'file'=>$productIDs[$i]['product_file'],
'image'=>$productIDs[$i]['product_image']
];
}else{
echo "$prd_id not found";
}
}
var_export($result);
Output:
array (
0 =>
array (
'id' => '10',
'file' => 'file10',
'image' => 'https://i.imgur.com/dXb6.png',
),
1 =>
array (
'id' => '20',
'file' => 'file20',
'image' => 'https://i.imgur.com/MuP8.png',
),
)
If I have the following arrays
arry1 = array(
101 => array(
'title1' => 'data',
'title2' => 'data',
'title3' => 'data'
),
102 => array(
'title1' => 'data',
'title2' => 'data',
'title3' => 'data'
),
.
.
.
);
arry2 = array(
101 => array(
'title4' => 'data',
'title5' => 'data',
'title6' => 'data'
),
102 => array(
'title4' => 'data',
'title5' => 'data',
'title6' => 'data'
),
.
.
.
);
and I want to change them into
arry3 = array(
101 => array(
'title1' => 'data',
'title2' => 'data',
'title3' => 'data',
'title4' => 'data',
'title5' => 'data',
'title6' => 'data'
),
102 => array(
'title1' => 'data',
'title2' => 'data',
'title3' => 'data',
'title4' => 'data',
'title5' => 'data',
'title6' => 'data'
),
.
.
.
);
Is there a simple function from php arrays to do this? If not, what do you believe would be the most efficient way to program this?
Thanks for any help,
Metropolis
EDITED
Sorry I updated the arrays to be the way they actually should be....array_merge_recursive gives me the following,
arry3 = array(
0 => array(
'title1' => 'data',
'title2' => 'data',
'title3' => 'data'
),
1 => array(
'title4' => 'data',
'title5' => 'data',
'title6' => 'data'
),
.
.
.
);
I need the 101, and 102 to stick, and I need the data to all be in the same lower level array....
I assume you want to add the latter array to the first. Therefore, use this:
array_merge_recursive(array1, array2);
... and it'll do exactly what you want.
EDIT:
As it seems, that my above solution is not entirely correct, use this:
<?
function array_merge_subarrays(array $array1, array $array2) {
$resultArray = array();
// The foreach instead of a plain for is to keep the specific values of the keys
foreach ($array1 as $key => $subarray) {
$resultArray[$key] = array_merge($subarray, $array2[$key]);
}
return $resultArray;
}
$arr1 = array(
101 => array(
'title1' => 'data',
'title2' => 'data',
'title3' => 'data'
),
102 => array(
'title1' => 'data',
'title2' => 'data',
'title3' => 'data'
)
);
$arr2 = array(
101 => array(
'title4' => 'data',
'title5' => 'data',
'title6' => 'data'
),
102 => array(
'title4' => 'data',
'title5' => 'data',
'title6' => 'data'
)
);
print_r(array_merge_subarrays($arr1, $arr2));
/*
OUTPUTS:
Array (
[101] => Array (
[title1] => data
[title2] => data
[title3] => data
[title4] => data
[title5] => data
[title6] => data
)
[102] => Array (
[title1] => data
[title2] => data
[title3] => data
[title4] => data
[title5] => data
[title6] => data
)
)
*/
?>