How to listSubscribe in Groups Mailchimp API 1.3 - php

As the example below shows how to call on the fields, my question is how to call a multiple checked checkbox. please give me an example
$merge_vars = array('FNAME'=>'Test', 'LNAME'=>'Account',
'GROUPINGS'=>array(
array('name'=>'Your Interests:', 'groups'=>'Bananas,Apples'),
array('id'=>22, 'groups'=>'Trains'),
)
);
I get a solution for this.
To get the multiple checked checkbox you need to do a looping and set it in array then change the array to a string.
if(!empty($_POST['listbox']))
{
foreach($_POST['listbox'] as $value => $val)
{
$values[] = $val;
}
$groups = implode(",", $values);
}
then set it in the merge_vars
$merge_vars = array('FNAME'=>'Test', 'LNAME'=>'Account',
'GROUPINGS'=>array(
array('name'=>'Your Interests:', 'groups'=> $groups)
)
);
Hope it helps :)

You must put the separated by commas but you must ensure that they have commas escaped:
$groups = array();
if(!empty($_POST['listbox'])) {
$interests = array();
foreach($_POST['listbox'] as $interest)
{
$interests[] = str_replace(',', '\,', $interest);
}
$groups = implode(",", $interests);
}
$merge_vars = array(
'FNAME'=>'Test',
'LNAME'=>'Account',
'GROUPINGS'=> array(
array(
'name'=>'Your Interests:',
'groups'=> $groups
),
array(
'id'=>22,
'groups'=>'Trains'
)
)
);
If you are sure that the interest string do not have commas you can just do this:
$groups = implode(',', $_POST['listbox']);

Related

Merge combinations of values from a 2d array and form non-standard delimited strings

I have the following array:
$array = array
(
'firstname' => array('Daniel', 'John'),
'lastname' => array('Smith', 'Larsson')
);
I want to turn it into:
$array = array('firstname=daniel:lastname=smith',
'firstname=daniel:lastname=larsson',
'firstname=john:lastname=smith',
'firstname=john:lastname=larsson');
Of course the array can have more names and also have more fields other than "firstname" and "lastname".
What would be the most optimal way to solve this?
Something like the following should work:
function combine($fields) {
if (!count($fields)) return array('');
$values = reset($fields);
$field = key($fields);
array_shift($fields);
$suffixes = combine($fields);
$options = array();
foreach ($values as $value) {
$options = array_merge($options, array_map(function($suffix) use($field, $value) {
return "$field=$value:$suffix";
}, $suffixes));
}
return $options;
}
You will probably have to adjust it though (like remove extra : in the end).

retrieve database values and then replace values with images outside of database

I cannot get my head around how to do this.
I have a mysql table called 'companies' that has a column called 'language'. This language column contains country abbreviations (en, de, es...).
Then I have flags saved in media/images/flags (en.png, de.png, es.png...).
I do not want to save the flags in db but I want to change db-country abbrevs. into flags inside the code. Below, I store the country abbrevs from db inside abbrevs. and try somehow to match them to array keys.
Model (platform_model):
public function get_country(){
$query = $this->db->query("SELECT language FROM companies");
return $query;
}
Controller:
public function set_flag(){
$this->load->model('platform_model');
$abbrevs = $this->platform_model->get_country();
$flags = array(
'de' => base_url('media/images/flags/de.png'),
'en' => base_url('media/images/flags/en.png'),
'it' => base_url('media/images/flags/it.png'),
'fr' => base_url('media/images/flags/fr.png'),
'es' => base_url('media/images/flags/fr.png'),
'pt' => base_url('media/images/flags/pt.png'),
'ru' => base_url('media/images/flags/ru.png'),
'ch' => base_url('media/images/flags/ch.png'),
'ja' => base_url('media/images/flags/ja.png')
);
//how can I compare/replace them from here?
Thank you for help.
Rather than array you can do something like..
<img src="media/images/flags/<? echo $county; ?>.png" />
Use str_replace, get the search elements which are the country codes (keys) and the replacement values which are the flags (values)
$replace = array_values($flags);
$search = array_keys($flags);
$string = str_replace($search, $replace, $string)
$arr = func_get_args();
$num = func_num_args();
$keys = array();
$i = 0;
for($i=0;$i<$num;++$i){
$keys = array_merge($keys, array_keys($arr[$i]));
}
$keys = array_unique($keys);
$merged = array();
foreach($keys as $key){
$merged[$key] = array();
for($i=0;$i<$num;++$i){
$merged[$key][] = isset($arr[$i][$key])?$arr[$i][$key]:null;
}
}

combine strings into an array in php

I have a web service to identify people and their functions from an external database that returns me a set of data if the login is successful. The data (that interests me right now) is separated in different strings as follow:
$groups="group1, group2, group3"
$functions="member, member, admin"
The first element of the string $groups corresponds to the first element of the $functions string.
We can have empty spots in the strings:
$groups="group1,, group3";
$functions="member,, admin";
What is the best way to combine them to obtain:
$usertype(
group1=>member,
group2=>member,
group3=>admin,
);
Then I plan to use array_search() to get the name of the group that corresponds to a function.
This is very trick especially when the first element is empty but here is a comprehensive solution
What you need is :
// Your Varriables
$groups = "group1,, group3";
$functions = "member,, admin";
// Break Into Array
$groups = explode(",", $groups);
$functions = explode(",", $functions);
// Combine both new Arrays and Output Result
$new = array_combine($groups, $functions);
print_r($new);
If you need to fix null values then :
Example :
// Your Varriables
$groups = "group1,, group3";
$functions = "member,, admin";
// Break Into Array
$groups = explode(",", $groups);
$functions = explode(",", $functions);
// Fix Null Values
$groups = fixNull($groups, true);
$functions = fixNull($functions);
// Combine both new Arrays and Output Result
$new = array_combine($groups, $functions);
print_r($new);
Output
Array
(
[group1] => member
[group2] => member
[group3] => admin
)
See Live DEMO
More Complex:
// Your Varriables
$groups = ",,, group3";
$functions = ",member,, admin";
// Fix Null Values
$groups = fixNull(explode(",", $groups), true);
$functions = fixNull(explode(",", $functions));
// Combine both new Arrays and Output Result
$new = array_combine($groups, $functions);
print_r($new);
Output
Array
(
[group4] => member
[group5] => member
[group6] => member
[group3] => admin
)
Live DEMO
Function Used
function fixNull($array, $inc = false) {
$ci = new CachingIterator(new ArrayIterator($array), CachingIterator::FULL_CACHE);
if ($inc) {
$next = array_filter($array);
$next = current($next);
$next ++;
} else {
$next = array_filter($array);
sort($next);
$next = end($next);
}
$next || $next = null;
$modified = array();
foreach($ci as $item) {
$modified[] = empty($item) ? trim($next) : trim($item);
if (! $ci->getInnerIterator()->current()) {
$item || $item = $next;
$next = $inc ? ++ $item : $item;
}
}
return $modified;
}
$groups = explode(",", $groups);
$functions = explode(",", $functions);
//then use the elements of the $groups array as key and the elements of the $functions array as the value
$merged = array_combine($groups, $functions);
Something along the lines of this should help:
$usertype = array_combine(explode(',', $groups), explode(',', $functions));
Use explode() to make arrays of your strings, and array_combine() to use one array as keys, the other one as values.
$groups = "group1, group2, group3";
$functions = "member, member, admin";
$usertype = array_combine(explode(", ", $groups), explode(", ", $functions));
Have you tried a explode($delimiter , $string) and then filter the array? I think that would be a good way of doing it:
$groups="group1,, group3";
$functions="member,, admin";
$groups_array = array_map('trim',explode(',', $groups));
$functions_array = array_map('trim',explode(',', $functions));
$final = array();
for ($i = 0; $i <= count($groups_array); $i++) {
$final[$groups_array[$i]] = $functions_array[$i];
}
$merged = array_combine($groups_array, $functions_array);
Demo
$groups="group1, group2, group3"
$functions="member, member, admin"
preg_match_all('/\w+/', $groups, $groups);
preg_match_all('/\d+/', $functions, $functions);
$final = array();
foreach ($groups[0] AS $key => $letter)
$final[] = $letter . '=>' . $functions[0][$key];
echo join(', ', $final);
explode and array_combine.
Note that you have a problem with whitespaces. Therefore use the following:
<?php
$groups="group1, group2, group3";
$functions="member, member, admin";
$groupArray = array_map(function($element){return trim($element);},
explode(',',$groups)); // split into array and remove leading and ending whitespaces
$functionArray = array_map(function($element){return trim($element);},
explode(',',$functions)); // split into array and remove leading and ending whitespaces
print_r(array_combine($groupArray,$functionArray));
?>
This will output:
Array
(
[group1] => member
[group2] => member
[group3] => admin
)
EDIT: removed trim for the possibility that the first element is empty.

array to string php

Hy every one I have this problem with an array I start like this...
$name = array($_POST['names']);
$nameId = array();
$query = mysql_query("SELECT id FROM types WHERE find_in_set (name, '$name')");
while($row = mysql_fetch_assoc($query)){
$nameId[] = array('ids' => $row['id'] );
}
which gives me arrays like this..
$name:
array('0'=>'name1,name2,name3')
$names:
array('0'=>array('ids'=>'61'), '1'=>array('ids'=>'6'), '2'=>array('ids'=>'1'))
how can I bring this in an string/form like this..
array('0'=>'61,6,1')
The idea is to save the ids to the Database.
Or is the a better more efficent way to get names from a form compare them with a database and get the ids back to save them to the Database?
many thanks in advance.
Change your assignment to this:
$nameId[] = $row['id'];
$name = array(name1,name2,name3);
$nameId = array();
$query = mysql_query("SELECT id FROM types WHERE find_in_set (name, '$name')");
while($row = mysql_fetch_assoc($query)){
//below line changed
$nameId[] = $row['id'] ;
}
$string = implode(',',$nameId);
Try this :
$array = array(0=>array(0=>'61'),1=>array(0=>'6'),2=>array(0=>'1'));
$result = implode(",",call_user_func_array('array_merge', $array));
Please note : Here all are numeric keys, so change your code to :
$nameId[] = array($row['id'] ); , remove key 'ids' from here
Output :
61,6,1
Thats what I think
$nameId[] = $row['id'];
$stringId = implode(',',$name);
Use following function that will loop through array and find ids key and merge it into other array and after that when you calling this function it will impload it.
function CustomFindJoinArray( $needly, $array )
{
$results = array();
foreach ( $array as $key => $value )
{
if ( is_array( $value ) )
{
$results = array_merge($results, foo( $needly, $value ));
}
else if ( $key == $needly )
{
$results[] = $value;
}
}
return $results;
}
echo implode( ",", CustomFindJoinArray( "ids", $your_array ) );
where $your_array will be array('0'=>array('ids'=>'61'), '1'=>array('ids'=>'6'), '2'=>array('ids'=>'1'))
OR More simple
foreach ($your_array as $key => $ids) {
$newArray[] = $array[$key]["ids"];
}
$string = implode(',', $newArray);
$ids = array();
foreach($nameId as $curr) {
$ids[] = $curr['ids'];
}
$str = "(".implode(",",$ids).")";

Modify syntax of this function to exclude several arrays instead of one

I'm a newbie to PHP and don't know how to modify the syntax of this function so that it can be used to exclude several arrays instead of only one. This code automatically INSERTS every value that's input in a form without having to specify the fields and excludes one array (called 'submit'), and is a slightly modified version of code that I found at http://www.abeautifulsite.net/blog/2007/10/inserting-an-array-into-a-mysql-database-table/
I have several arrays which are being posted that I want to exclude from my INSERT function since they are either being processed and inserted separately or trigger where the user is redirected once the form is processed.
function mysql_insert_array($db, $data, $exclude = array()) {
$fields = $values = array();
if( !is_array($exclude) ) $exclude = array($exclude);
foreach( array_keys($data) as $key ) {
if( !in_array($key, $exclude) ) {
$fields[] = "`$key`";
$values[] = "'" . mysql_real_escape_string($data[$key]) . "'";
}
}
$fields = implode(",", $fields);
$values = implode(",", $values);
if( mysql_query("INSERT INTO `$db` ($fields) VALUES ($values)") ) {
} else {
return array( "mysql_error" => mysql_error() );
}
}
$result = mysql_insert_array("db", $_POST, "submit");
The exclude argument could be an array of array:
function mysql_insert_array($db, $data, $excludes = array()) {
$fields = $values = array();
if( !is_array($excludes) ) $excludes = array($excludes);
foreach($excludes as $exclude ) {
$data = array_diff_assoc($data, $exclude);
}
foreach( array_keys($data) as $key ) {
$fields[] = "`$key`";
$values[] = "'" . mysql_real_escape_string($data[$key]) . "'";
}
$fields = implode(",", $fields);
$values = implode(",", $values);
if( !mysql_query("INSERT INTO `$db` ($fields) VALUES ($values)") ) {
return array( "mysql_error" => mysql_error() );
}
}
Then, you could use it like this :
$array1 = array('toto', 'titi', 'tata');
$array2 = array('submit', 'foo');
$parent_array = array ($array1, $array2);
$result = mysql_insert_array("db", $_POST, $parent_array);

Categories