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.
Related
This question already has answers here:
PHP getting sum of values group by key in array [duplicate]
(2 answers)
grouping of array in PHP [duplicate]
(2 answers)
How to sum array value of duplicate data
(5 answers)
Closed 1 year ago.
can someone explain me how to multiply the value from this code? i tried but still cant solve this
arrayK(
0 => array(
'name'=> AA,
'value' => 2.00,
),
1 => array(
'name' => AA,
'value' => 1.82,
),
2 => array(
'name' => BB,
'value' => 2.20,
),
3 => array(
'name' => AA,
'value' => 4.20,
),
4 => array(
'name' => BB,
'value' => 4.20,
),
);
the answer should back to array with value already multiply where it has same name
newArray(
0 => array(
'name'=> AA,
'value' => ...,
),
1 => array(
'name' => BB,
'value' => ....,
),
);
To multiply duplicate value and get new array:
$array=array(
0 => array(
'name'=> AA,
'value' => 2.00,
),
1 => array(
'name' => AA,
'value' => 1.82,
),
2 => array(
'name' => BB,
'value' => 2.20,
),
3 => array(
'name' => AA,
'value' => 4.20,
),
4 => array(
'name' => BB,
'value' => 4.20,
),
);
$result = array();
foreach ($array as $val) {
if (!isset($result[$val['name']]))
$result[$val['name']] = $val;
else
$result[$val['name']]['value'] *= $val['value'];
}
$result = array_values($result); // reindex array
echo "<pre>";
print_r($result);
You can try following solution.
$arrayK = array(
0 => array(
'name'=> "AA",
'value' => 2.00,
),
1 => array(
'name' => "AA",
'value' => 1.82,
),
2 => array(
'name' => "BB",
'value' => 2.20,
),
3 => array(
'name' => "AA",
'value' => 4.20,
),
4 => array(
'name' => "BB",
'value' => 4.20,
),
);
$temp = array_values((array_unique(array_column($arrayK, 'name'))));
$arrayY = [];
for( $i = 0; $i < count($temp); $i++ ) {
foreach( $arrayK as $key => $val ) {
if( $val['name'] == $temp[$i] ) {
$arrayY[$i] = [
'name' => $val['name'],
'value' => isset($arrayY[$i]['value']) ? $val['value'] * $arrayY[$i]['value'] : $val['value']
];
}
}
}
print_r($arrayY);
Out put
Array
(
[0] => Array
(
[name] => AA
[value] => 15.288
)
[1] => Array
(
[name] => BB
[value] => 9.24
)
)
You can play with the code Here
I have two arrays.
$primary = array(
[0] => array(
'name' => 'PPT Shop',
'place_id' => '1000',
'category' => '220,221',
'address' =>
),
[1] => array(
'name' => 'Meat Shop',
'place_id' => '1001',
'category' => '220,221'
'address' =>
),
[2] => array(
'name' => 'Bikini Shop',
'place_id' => '1010',
'category' => '100,102'
'address' =>
),
[3] => array(
'name' => 'Knife Shop',
'place_id' => '1012',
'category' => '1,3'
'address' =>
)
)
$moredata = array(
[0] => array(
'id' => '1000',
'category' => '900,901'
'address' => '35 Lawrence Park',
'phone' => '9000000099'
),
[1] => array(
'id' => '1001',
'category' => '909,300'
'address' => '39 Park Avenue',
),
[2] => array(
'id' => '1010',
'category' => '50,45'
'address' => '35 Trump Park',
'phone' => '8900000099'
)
)
I want to compare each data of $moredata with each data of $primary, and check if the place_id from $primary exists in $moredata. If it matches, then the corresponding records of that particular key will be updated. For example,
$newPrimary = array(
[0] => array(
'name' => 'PPT Shop',
'place_id' => '1000',
'category' => '220,221,900,901',
'address' => '35 Lawrence Park',
'phone' => '9000000099'
),
[1] => array(
'name' => 'Meat Shop',
'place_id' => '220,221,1001',
'category' => '220,221,909,300',
'address' => '39 Park Avenue',
),
[2] => array(
'name' => 'Bikini Shop',
'place_id' => '1010',
'category' => '100,102,50,45'
'address' => '35 Trump Park',
'phone' => '8900000099'
),
[3] => array(
'name' => 'Knife Shop',
'place_id' => '1012',
'category' => '1,3'
'address' =>
)
)
place_id(1000) of primary matches with id(1000) of moredata, so the place_id(1000) of newPrimary is like this:-
array(
'name' => 'PPT Shop',
'place_id' => '1000',
'category' => '220,221,900,901', // the categories get concated
'address' => '35 Lawrence Park',
'phone' => '9000000099'
)
However, for place_id(1001) of primary doesn't have phone field, so the id(1001) of newPrimary is like this:-
array(
'name' => 'Meat Shop',
'place_id' => '1001',
'category' => '220,221,909,300',
'address' => '39 Park Avenue',
)
place_id(1012) has no match, so it remains unchanged.
How can I create an array similar to newPrimary? It would be better if we can append the fields from moredata to the corresponding record from primary. I achieved the same using double foreach loop. I want to achieve this is a way to have lesser execution time.
foreach($primary as $key_fs => $prm)
{
foreach($moredata as $key_place => $pc)
{
if($prm['place_id'] == $pc['id'])
{
if(isset($pc['address']) && !empty($pc['address']))
$primary[$key_fs]['address'] = $pc['address'];
if(isset($pc['phone']) && !empty($pc['phone']))
$primary[$key_fs]['phone'] = $pc['phone'];
if(isset($pc['category']) && !empty($pc['category']))
$primary[$key_fs]['category'] .= ','.$pc['category'];
break;
}
}
}
Note:- The two arrays will have same dimension, but may not have same order.
You can use array_column to make the 2 arrays into multidimensional array. Use array_replace_recursive to merge the arrays.
You can use array_values if you want a simple array instead of multi dimensional output.
$primary = //Your array
$moredata = //Your array
$result = array_replace_recursive (array_column($primary, null, 'id') ,array_column($moredata, null, 'id') );
$result = array_values($result); //To convert the multidimensional array to simple array
Update:
You can use array_column to make a temp array for $moredata. This will make it easier to check if the id exist. Use the foreach to loop thru the array. If id exist on $moredata, simply concatenate the category.
$newMoreData = array_column($moredata, null, 'id');
$newPrimary = array();
foreach( $primary as $val ) {
if ( isset( $newMoreData[$val['place_id']] ) ) {
$temp = array_merge( $val, $newMoreData[$val['place_id']] );
$temp['category'] = $val['category'] . ',' . $newMoreData[$val['place_id']]['category'];
unset($temp['id']);
$newPrimary[] = $temp;
} else {
$newPrimary[] = $val;
}
}
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',
),
)
I want to remove a child array from a multi-dimensional array in case a duplicate value found for a particular key. The answer(s) here didn't work at all. The answer here works, however, for large amount of arrays, that gets pretty slower. Looking for a cleaner and faster solution.
Example PHP Array
$args = array();
$args[] = array(
'section' => array(
'id' => 'section1',
'name' => 'Section 1',
),
'name' => 'Shortcode Name',
'action' => 'shortcodeaction',
'icon' => 'codeicon',
'image' => 'codeimage',
);
$args[] = array(
'section' => array(
'id' => 'section2',
'name' => 'Section 2',
),
'name' => 'Shortcode2 Name',
'action' => 'shortcodeaction2',
'icon' => 'codeicon2',
'image' => 'codeimage2',
);
$args[] = array(
'section' => array(
'id' => 'section3',
'name' => 'Section 3',
),
'name' => 'Shortcode3 Name',
'action' => 'shortcodeaction3',
'icon' => 'codeicon3',
'image' => 'codeimage3',
);
$args[] = array(
'section' => array(
'id' => 'section1',
'name' => 'Section 4',
),
'name' => 'Shortcode4 Name',
'action' => 'shortcodeaction4',
'icon' => 'codeicon4',
'image' => 'codeimage4',
);
$args[] = array(
'section' => array(
'id' => 'section5',
'name' => 'Section 5',
),
'name' => 'Shortcode5 Name',
'action' => 'shortcodeaction5',
'icon' => 'codeicon5',
'image' => 'codeimage5',
);
$sections = array();
foreach ( $args as $arg ) {
$sections[] = $arg['section'];
}
And, the print_r($sections) result.
Array
(
[0] => Array
(
[id] => section1
[name] => Section 1
)
[1] => Array
(
[id] => section2
[name] => Section 2
)
[2] => Array
(
[id] => section3
[name] => Section 3
)
[3] => Array
(
[id] => section1
[name] => Section 4
)
[4] => Array
(
[id] => section5
[name] => Section 5
)
)
Both Array[0] and Array[3] has the same value for the key id, therefor the entire Array[3] has to be removed in my case to avoid duplicates.
This is working for me though, but it gets really slow when there are 100s or more arrays.
$knownIds = array();
foreach( $sections AS $key=>$item ) {
if( array_key_exists($item['id'], $knownIds) === true ) {
unset( $sections[$key] );
} else {
$knownIds[$item['id']] = $key;
}
}
$sections = array_values($sections);
Tried several answers here in StackOverflow (including this), but none of them helped in my case.
Thanks
You can modify the whole using array_column and array_filter -
//get all the sections value
$section = array_column($args, 'section');
//store ids in temp array
$idArray = array_unique(array_column($section, 'id'));
//filter the array having unique id
$uniqueSections = array_filter($section, function ($key, $value) use ($idArray) {
return in_array($value, array_keys($idArray));
}, ARRAY_FILTER_USE_BOTH);
var_dump($uniqueSections);
For PHP <5.5
$section = array_map(function($args) {
return $args['section'];
}, $args);
$idArray = array_unique(array_map(function($section){return $section['id'];}, $section));
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.