MailChimp API member-info issue - php

I need to retrieve all users infos via api, looking for it in the documentation I found this:
http://apidocs.mailchimp.com/api/2.0/lists/member-info.php
This is my code:
$params = array(
'id' => $list_id,
'emails' => array(
'euid' => $member_id,
),
);
$infos = $this->MailChimp->call('lists/member-info', $params);
print_r($infos);
and this is the result:
Array ( [success_count] => 0 [error_count] => 1 [errors] => Array ( [0] => Array ( [email] => 63a885b7cf [error] => "email" should be a struct [code] => -100 ) ) [data] => Array ( ) )
What does " "email" should be a struct " means?

This also works and is slightly simpler:
$result = $MailChimp->call('lists/member-info', array(
'id' => $list_id,
'emails' => array(array('email'=>$email))
));
The above example uses this API: https://github.com/drewm/mailchimp-api/

Solved!
My $params array was wrong.
MailChimp need an array format in this way:
$params = array(
'id' => $list_id,
'emails' => array(
0 => array(
'euid' => $member_id,
),
),
);

Mine does this as follows:
array(
'0' => array('email' => $mail1)
'1' => array('email' => $mail2)
...
...
);
Thanks,

Related

Change query response array value using MongoDB and PHP

I am new in MongoDB. I have created query in MongoDB using PHP. My query is as follows:
$date_start = new MongoDate(strtotime("2016-06-08T18:30:00.000Z"));
$default_date = new MongoDate(strtotime("1970-01-01T00:00:00.000Z"));
$pipeline = array(
array('$project' => array(
'MainsPower' => 1,
'EventTS' => 1
)),
array('$unwind' => array(
'path' => '$MainsPower',
'includeArrayIndex' => "arrayIndex",
'preserveNullAndEmptyArrays' => true
)),
array('$match' => array(
'$and' => array(
//array('EventTS' => array('$gt' => $date_start)),
//array('PanelID' => 'A00911'),
array("MainsPower" => array('$ne' => null))
)
)),
array(
'$project' => array(
'MainsPower' => 1,
'_id' => 0,
'EventTS' => array(
'$add' => array(
array('$subtract' => array('$EventTS', $default_date)),
array('$multiply' => array(60000, '$arrayIndex'))
)
)
)
),
);
$result = $collection - > aggregate($pipeline);
Output of query is as below
Array
(
[0] => Array(
[EventTS] => 1497033900000[MainsPower] => 204
)
[1] => Array(
[EventTS] => 1497034800000[MainsPower] => 204
)
[2] => Array(
[EventTS] => 1497035700000[MainsPower] => 204
)
)
But, I want output as below because while plotting graph I need data in below format I searched many solution and I tried to apply the same but failed to get records in required format
Array
(
[0] => Array(
[1497033900000, 204]
)
[1] => Array(
[1497034800000, 204]
)
[2] => Array(
[1497035700000, 204]
)
)
Use the below code:
$formattedResponse = array();
foreach($mongoOutput as $out){
$formattedResponse[] = array($out["EventTS"],$out["MainsPower"]);
}
where mongoOutput is the default output you are getting from mongo, and formattedResponse is the variable in which you will get the output in desired format.
Please change variable names as per your needs.

convert cavtivedataprovider to array with specific form

im using yii1 on my application.
i want to convert from cActivedataProvider to array
this is the code
$dataSS = new CActiveDataProvider('category', array(
'criteria' => array(
'condition' => 'menu=:menu',
'params' => array(':menu' => $menu),
),
'pagination' => false
));
$dataMenu = array();
foreach ($dataSS->getData() as $record) {
$dataMenu[] = array(
'label' => $record->name,
'url' => '#',
);
}
this is the result :
Array (
[0] => Array ( [label] => Food and Drink [url] => # )
[1] => Array ( [label] => Sleman [url] => # )
)
the result that i expected :
Array (
Array ( 'label' => 'Food and Drink', 'url' => '#' ) ,
Array ( 'label' => 'Sleman', 'url' => '#' ) ,
)
any suggestion?
Finally get the answer,
this is really my bad because i call the function on wrong way.
This is the wrong way :
'items' =>array(Category::model()->getMenu("2");),
and this is the correct way :
'items' =>Category::model()->getMenu("2"),

append data to JSON object issue

I am trying to add data to JSON object. The output result doesn't show appended data "people3". Could you please let me know if I am doing right.
$postArray = array(
"persons" => array(
"person" => array(
"i_date"=> $DatabaseDate,
"i_location"=>$_POST["location"],
"i_summary"=>$_POST["summary"]
),
"people" => array(
"people1"=> array(
"first_name"=> $_POST["first-1"],
"last_name"=>$_POST["last-1"]
),
"people2"=> array(
"first_name"=>$_POST["first-2"],
"last_name"=>$_POST["last-2"],
)
)
)
);
array_push($postArray['people'],
array(
"people3"=> array(
"first_name"=>$_POST["first-2"],
"last_name"=>$_POST["last-2"],
)
));
var_dump(json_encode( $postArray ));
Updated code:
array_push($postArray["persons"]['people'],
array("people3"=> array(
"first_name"=>$_POST["first-2"],
"last_name"=>$_POST["last-2"],
)));
Check this:
$postArray = array(
"persons" => array(
"person" => array(
"i_date"=> $DatabaseDate,
"i_location"=>$_POST["location"],
"i_summary"=>$_POST["summary"]
),
"people" => array(
"people1"=> array(
"first_name"=> $_POST["first-1"],
"last_name"=>$_POST["last-1"]
),
"people2"=> array(
"first_name"=>$_POST["first-2"],
"last_name"=>$_POST["last-2"],
)
)
)
);
$postArray["persons"]['people']['people3']=
array(
"first_name"=>$_POST["first-2"],
"last_name"=>$_POST["last-2"],
);
var_dump(json_encode( $postArray ));
Output in php: print_r :-
Array
(
[persons] => Array
(
[person] => Array
(
[i_date] =>
[i_location] =>
[i_summary] =>
)
[people] => Array
(
[people1] => Array
(
[first_name] =>
[last_name] =>
)
[people2] => Array
(
[first_name] =>
[last_name] =>
)
[people3] => Array
(
[first_name] =>
[last_name] =>
)
)
)
)
As mentioned by others, that is correct as well.
array_push($postArray['persons']['people'],
array(
"people3"=> array(
"first_name"=>$_POST["first-2"],
"last_name"=>$_POST["last-2"],
)
));

MongoDB + PHP: How to push item to array with a spcecific key?

In MongoDB-PHP I am using the following example code to push a new entry to the end of an array inside a collection...
$data = array(
"domain"=>"superduperyoyo.com",
"number"=>123,
"week"=>5,
"year"=>2012
);
$db->domains->save(
array( 'someid' => $someid),
array( '$push' => array( 'data' => $data ))
);
This returns keys like 0,1,2,3....
ie.
[someid] => somesupercoolid123
[data] => Array
(
[0] => Array
(
[domain] => superduperyoyo.com
[number] => 123
[week] => 5
[year] => 2012
)
[1] => Array(...)
[2] => Array(...)
)
What I want to do is store YearWeekNumber as the key like this...
[someid] => somesupercoolid123
[data] => Array
(
[201205123] => Array
(
[domain] => superduperyoyo.com
[number] => 123
[week] => 5
[year] => 2012
)
[201206123] => Array(...)
[201207123] => Array(...)
)
How do you save/update the key along with the new entry? I am assuming you can't use $push. That you just use .save or .update but how do you pass the key?
You'd do this by using $set:
$data = array(
"domain"=>"superduperyoyo.com",
"number"=>123,
"week"=>5,
"year"=>2012
);
$db->domains->update(
array( 'someid' => $someid),
array( '$set' => array( 'data' => array( 201205123 => $data )))
);
I would however not recommend doing this. It's better to set another key with this "201205123" value as otherwise you wouldn't be able to do range queries on this value or set an index.
$data = array(
"domain"=>"superduperyoyo.com",
"number"=>123,
"week"=>5,
"year"=>2012
);
$update = array(
'$push' => array('data.201205123' => $data )
);

Removing unnecessary nested index in array (generated by Cake PHP)

The following question can either be solved by probably changing my use of the find method in Cake PHP OR using some PHP function. I would prefer to solve it using Cake but it doesn't matter too much. I have any array like this:
Array
(
[0] => Array
(
[Model] => Array
(
[id] => 14
[foo] => bar
)
)
[1] => Array
(
[Model] => Array
(
[id] => 15
[foo] => something
)
) .............
I just want to remove the Model index and just use the numeric one. The following function generated this array:
$arr = $this->Model->find('all', array('contain' => false ) );
I probably need to change the 'contain' part of the call. Basically, in addition to the data that appears under each Model index, I also have a second and third model and the contain = false just restricts Cake from getting data from the current model (Model).
If I understand your question correctly, I think CakePHP's Set::combine function will help http://book.cakephp.org/2.0/en/core-utility-libraries/set.html#Set::combine :
$result = Set::combine($your_array, '{n}.Model.id', '{n}.Model.data');
This will result in:
Array
(
[14] => Array
(
[foo] => bar
)
[15] => Array
(
[foo] => something
)
)
you have to write your own piece of code to modify your array , here is a function that will do what you want (you can improve it)
function reformArray($array,$modelName)
{
foreach($arr as $key => $value)
{
$newArr[] = $arr[$key][$modelName];
}
return $newArr;
}
you have to pass your array and the Model Name to this function and will return you the result
$a = array(
array(
'User' => array(
'id' => 2,
'group_id' => 1,
'Data' => array(
'user' => 'mariano.iglesias',
'name' => 'Mariano Iglesias'
)
)
),
array(
'User' => array(
'id' => 14,
'group_id' => 2,
'Data' => array(
'user' => 'phpnut',
'name' => 'Larry E. Masters'
)
)
),
array(
'User' => array(
'id' => 25,
'group_id' => 1,
'Data' => array(
'user' => 'gwoo',
'name' => 'The Gwoo'
)
)
)
);
RUN THIS TO REMOVE THE MODEL NAME USER
Set::extract($a, '{n}.User');
WILL RETURN THIS
$a = array(
array(
'id' => 2,
'group_id' => 1,
'Data' => array(
'user' => 'mariano.iglesias',
'name' => 'Mariano Iglesias'
)
),
array(
'id' => 14,
'group_id' => 2,
'Data' => array(
'user' => 'phpnut',
'name' => 'Larry E. Masters'
)
),
array(
'id' => 25,
'group_id' => 1,
'Data' => array(
'user' => 'gwoo',
'name' => 'The Gwoo'
)
)
);
In CakePHP 2:
$data = $this->Model->find('all');
$data = Set::extract('/Model/.', $data );
You can achieve this result by foreach also
foreach ($data as $k => &$t) {
$t = $t['Model']
}
It will be much easier with Object. Change the Array with Object.
Actually I had faced the same problem with nested array And I found the solution with Set::map($array);
If you don't want the model_name as nested array, You can prefer this solution.
$data = array(
array(
"IndexedPage" => array(
"id" => 1,
"url" => 'http://blah.com/',
'hash' => '68a9f053b19526d08e36c6a9ad150737933816a5',
'get_vars' => '',
'redirect' => '',
'created' => "1195055503",
'updated' => "1195055503",
)
),
array(
"IndexedPage" => array(
"id" => 2,
"url" => 'http://blah.com/',
'hash' => '68a9f053b19526d08e36c6a9ad150737933816a5',
'get_vars' => '',
'redirect' => '',
'created' => "1195055503",
'updated' => "1195055503",
),
)
);
$mapped = Set::map($data);
/* $mapped now looks like: */
Array
(
[0] => stdClass Object
(
[_name_] => IndexedPage
[id] => 1
[url] => http://blah.com/
[hash] => 68a9f053b19526d08e36c6a9ad150737933816a5
[get_vars] =>
[redirect] =>
[created] => 1195055503
[updated] => 1195055503
)
[1] => stdClass Object
(
[_name_] => IndexedPage
[id] => 2
[url] => http://blah.com/
[hash] => 68a9f053b19526d08e36c6a9ad150737933816a5
[get_vars] =>
[redirect] =>
[created] => 1195055503
[updated] => 1195055503
)
)
you may do this :
$tmp = $this->Model->find('all', array('contain' => false ) );
$arr = $tmp['ModelName'];

Categories