PHP combine two arrays in foreach loop - php

I need guidance on how to combine two arrays in foreach loop. I have a list of checkboxes that will add user to corresponding list.
To grab my checkboxes as an array using :
<input type="checkbox" name="checkbox_list[]" value="Austin Metro">
<input type="checkbox" name="checkbox_list[]" value="Central Austin">
<input type="checkbox" name="checkbox_list[]" value="Georgetown">
...
Here is my loop:
foreach($_POST['checkbox_list'] as $check) {
$myValues = array('Austin Metro','Central Austin','Georgetown','Lake Travis | Westlake','Leander | Cedar Park','Northwest Austin','Round Rock | Pflugerville | Hutto','San Marcos | Buda | Kyle','Southwest Austin','DFW Metro','Frisco','Grapevine | Colleyville | Southlake','McKinney','Plano','Houston Metro','Conroe | Montgomery','Cy-Fair','Katy','Pearland | Friendswood','Spring | Klein','Sugar Land | Missouri City','Tomball | Magnolia','The Woodlands');
$myIds = array('3','4','5','6','7','8','9','10','11','25','12','13','14','15','24','16','17','18','19','20','21','23','22');
if (isset($check) && array_search($check, $myValues) !== false){
$index = array_search($check, $myValues);
$indeed_mail->indeed_wysija_subscribe( $myIds[$index], $email );
}
}
Currently it is adding user to the right list but is sending one email confirmation per list, instead of merging all subscribed lists in one single email.(in other words if user selects "Austin" & "Houston" lists, it will get one email for "Austin" and one email for "Houston")
Any suggestions on how to fix? Thanks!
Here is function that add subscriber and sends email confirmation:
public function indeed_wysija_subscribe( $listId, $e_mail, $first_name='', $last_name='' ){
$user_data = array(
'email' => $e_mail,
'firstname' => $first_name,
'lastname' => $last_name);
$data = array(
'user' => $user_data,
'user_list' => array('list_ids' => array($listId))
);
$helper = &WYSIJA::get('user', 'helper');
if($helper->addSubscriber($data)) return 1;
else return 0;
}

You should define the array with checkbox names/IDs outside of your loop, and use a more appropriate structure for them:
$map = array (
3 => 'Austin Metro',
4 => 'Central Austin',
5 => 'Georgetown',
6 => 'Lake Travis | Westlake',
7 => 'Leander | Cedar Park',
8 => 'Northwest Austin',
9 => 'Round Rock | Pflugerville | Hutto',
10 => 'San Marcos | Buda | Kyle',
11 => 'Southwest Austin',
25 => 'DFW Metro',
12 => 'Frisco',
13 => 'Grapevine | Colleyville | Southlake',
14 => 'McKinney',
15 => 'Plano',
24 => 'Houston Metro',
16 => 'Conroe | Montgomery',
17 => 'Cy-Fair',
18 => 'Katy',
19 => 'Pearland | Friendswood',
20 => 'Spring | Klein',
21 => 'Sugar Land | Missouri City',
23 => 'Tomball | Magnolia',
22 => 'The Woodlands',
);
Now you can even remove the loop completely and continue with this code:
// take from map only those entries that have been checked:
$checks = array_intersect($map, $_POST['checkbox_list']);
// pass the IDs of those values to the emailing class:
$indeed_mail->indeed_wysija_subscribe( array_keys($checks), $email );
You just now need to adapt that indeed_wysija_subscribe so it can deal with arrays. There is only one line to change:
'user_list' => array('list_ids' => $listId)
Notice that array($listId) becomes just $listId because that argument is now already an array.

PHP have array_combine function
http://php.net/manual/en/function.array-combine.php

Ignoring the Title of your questions and focusing on the question asked.
Currently it is adding user to the right list but is sending one email confirmation per list, instead of merging all subscribed lists in one single email.
You need to collate the user ids selected, before you pass it into the subscribe function.
Currently your calling the subscribe function, indeed_wysija_subscribe, inside the foreach loop foreach($_POST['checkbox_list'] as $check) {.
You need to move the subscribe call outside the loop and pass in an array of $listId.
This may require you to modify the indeed_wysija_subscribe function to support the array. Or there may be another function in the library.
I am not sure what is part of the library or not.
Also, you can perform the mapping between city name to numeric id in more direct manner.
$myValues = array('Austin Metro','Central Austin','Georgetown');
$myIds = array('3','4','5','6');
$cityToIdMap = array(
'Austin Metro'=>'3',
'Central Austin'=>'4',
'Georgetown'=>'5'
);
$subscriberIds = array();
foreach($_POST['checkbox_list'] as $check) {
if(!empty($check) && isset($cityToIdMap[$check])){
$subscriberIds[] = $cityToIdMap[$check];
}
}

Related

Raking Result of search query

Rank Result data set
i am building meta search tool which get results from different sources in response to user query. i have saved the results in array of object having information like title, description , release date etc.before showing it on interface i want to rank them, so that most relevant result should be on top just like search engines. but i am new to ranking and don't know about it. so kindly guide me in this matter which ranking algorithm i should follow or any useful link for help.
I think you need to add "weighting" column (can be null) on your array of Object and just before to show the elements you need to loop on weighting if they got one.
Then your results will be shown in first and if no weight just the normal display.
Here an example :
<?php
//Function to compare weightings
function cmp($a, $b) {
if ($a == $b) {
return 0;
}
return ($a < $b) ? -1 : 1;
}
$searches = array(
'songs'=> array(
0 => array(
'title' => 'coldplay',
'weight'=> 3
),
1 => array(
'title' => 'eminem',
'weight'=> 2
),
2 => array(
'title' => 'rihanna',
'weight'=> 2
),
3 => array(
'title' => 'shakira',
'weight'=> 1
),
4 => array(
'title' => 'nirvana',
'weight'=> null
),
5 => array(
'title' => 'acdc'
)
),
);
//this foreach is used to apply the weighting on itterations
foreach($searches['songs'] as $key => $search){
//if no weight of weight is null
if(array_key_exists('weight', $search) && $search['weight']){
$array_by_weight[$key]['weight'] = $search['weight'];
$array_by_weight[$key]['title'] = $search['title'];
}else{
$array_by_weight[$key]['weight'] = 5; //Value max of weighting
$array_by_weight[$key]['title'] = $search['title'];
}
}
//We use our function to compare and sort our array
uasort($array_by_weight, 'cmp');
//display
foreach($array_by_weight as $songs){
echo $songs['title'].' | ';
echo $songs['weight'].PHP_EOL;
}
Output :
shakira | 1
eminem | 2
rihanna | 2
coldplay | 3
acdc | 5
nirvana | 5
I hope it can help you.

Possible to break statement in foreach loop?

I have two tables (user,user_group). Where user table contains
------------+-------------+------------------+
| user_id | user_name | user_group(id) |
------------+-------------+------------------+
and user_group table contains
+-----------------+-------------------+
| user_group_id | user_group_name |
+-----------------+-------------------+
Now I try to join them without using JOIN query. To solve this problem I have used double foreach() loop, but no result returns. I can't use break statement. How can I get the desired result using a loop? The desired result would be:
+-----------+-------------+-------------------+
| user_id | user_name | user_group_name |
+-----------+-------------+-------------------+
What I have so far:
public function user_get_item()
{
$this->db->select('*');
$this->db->from('users');
$results=$this->db->get()->result_array();
$this->db->select('*');
$this->db->from('user_group');
$group_data=$this->db->get()->result_array();
foreach($results as $v_results)
{
foreach($group_data as $v_group_data)
{
if($v_results['user_group']==$v_group_data['user_group_id'])
{
$v_results['user_group']=$v_group_data['user_group_name'];
}
}
}
return $results;
}
I think what you are trying to do is changing the group id identifier by the group name in the current row if they match. So, replace:
$results['user_group']=$v_group_data['user_group_name'];
by:
$v_results['user_group']=$v_group_data['user_group_name'];
Anyway, what's wrong with joins? Making SQL queries will be always faster than a polynomic loop.
Presumably you would have this:
Array(
0 => Array(
[user_id] => 1,
[user_name] => John Doe,
[user_group] => 200
),
1 => Array(
[user_id] => 2,
[user_name] => Jane Doe,
[user_group] => 100
)
)
And:
Array(
0 => Array(
[user_group_id] => 100,
[user_group_name] => Admin
),
1 => Array(
[user_group_id] => 200,
[user_group_name] => Web User
)
)
So make a new array for the group so it looks like this (ID as key, Name as value):
Array(
[100] => Admin,
[200] => Web User
)
To do that:
public function getUserGroups()
{
$rows = array();
$this->db->select('*');
$this->db->from('user_group');
$group_data=$this->db->get()->result_array();
foreach($group_data as $results)
{
$rows[$results['user_group_id']] = $results['user_group_name'];
}
return $rows;
}
Then you just assign in the other method:
public function userGetItem()
{
# Get all the user groups
$groupData = $this->getUserGroups();
# Get your original user list
$this->db->select('*');
$this->db->from('users');
$results=$this->db->get()->result_array();
$row = array();
foreach($results as $users) {
$row[] = array(
'user_id' => $users['user_id'],
'user_name' => $users['user_name'],
# Here you assign the group name from the user groups array
'user_group_name' => $groupData[$users['user_group']]
);
}
return $row;
}
Anyway, I think JOIN would be faster/better, but this should get you the results you are looking for, though I have not tested it, it should be close.

php sort merge arrays

I have a list of (xml) items, each item has a category and a name:
Category | Name
1 | Joe
2 | Carol
3 | Bruce
1 | Michael
1 | Alan
2 | Brian
I want to sort the names ascending within categories descending like so:
Category | Name
3 | Bruce
2 | Brian
2 | Carol
1 | Alan
1 | Joe
1 | Michael
with the aim of creating a Select dropdown on a web page with each category as an OptGroup and the names sorted within the OptGroup.
I have very little experience with PHP, I think I need to sort merge arrays, but after many hours trying to understand how to, I'm not making much progress.
Any help greatly appreciated
$data[] = array('category' => 1, 'name' => 'Joe');
$data[] = array('category' => 2, 'name' => 'Carol');
$data[] = array('category' => 3, 'name' => 'Bruce');
$data[] = array('category' => 1, 'name' => 'Michael');
$data[] = array('category' => 1, 'name' => 'Alan');
$data[] = array('category' => 2, 'name' => 'Brian');
<?php
// Obtain a list of columns
$category =array();
$name =array();
foreach ($data as $key => $row) {
$category[$key] = $row['category'];
$name[$key] = $row['name'];
}
// Sort the data with category descending, name ascending
// Add $data as the last parameter, to sort by the common key
array_multisort($category, SORT_DESC, $name, SORT_ASC, $data);
echo '<pre>'; print_r($category);
?>
Hi thanks to everybody for your help including all the other Stackoverflow questions which I googled to piece this together, not sure how I did it, bit of a case of an infinite number of monkeys or should that be newbies, anways here's what worked for me.
Sorts names ascending within contributions descending
function compare($a, $b) {
if ($a['contribution'] == $b['contribution']) {
return ($a['name'] < $b['name']) ? -1 : 1;
} else {
return ($b['contribution'] - $a['contribution']);
}
}
usort($ads_by_cotribution, 'compare');
It's all magic to me, that's the beauty of it. I didn't include the case of names being equal
'cause there shouldn't be any and as I understand equal values would still stay together just in a different order. I'd like to understand how these functions work, do they continuously walk through the array until there's no change in the order - what we used to call a bubble sort? Does ($b['contribution'] - $a['contribution']) indicate which is the larger? Finley is there a difference between sorting contributions - a numeric field and names - an alpha field?
$name = array("Joe", "Carol", "Bruce", "Michael","Alan","Brian");
sort($name);
php-in built function sort will produce desire result

Regex from rules stored in a database for ASTERISK using PHP

I'm trying to create a kind of REGEX with dynamic variables
with PHP using some pre-defined values stored in a Database table, in order to
validate a Dial String from Asterisk.
The database has
a table for outgoing rules, which each user may create to be applied to his/her extension.
Variables in PHP can be like this:
$N = '[23456789]';
$Z = '\d*'; //(any digit, any occurrence)
$X = '[0-9]';
$x = '[0-9]';
numbers in the rule will be considered as number itself
symbols in the rule will be considered as symbol itself (only * and # will be accepted in the database as symbols)
The table is something like this:
+----+-----------+-------+-------------+------------+----------+-----------+--------+--------+
| id | extension | order | description | ruledigits | ruletype | subtract | prefix | suffix |
+----+-----------+-------+-------------+------------+----------+-----------+--------+--------+
| 1 | 1005 | 1 | | 9XX | Block | null | null | null |
| 2 | 1005 | 2 | | 302NXXXXXX | Mod | null | 1 | null |
| 3 | 2005 | 1 | | 00Z | Mod | 2 | 011 | null |
+----+-----------+-------+-------------+------------+----------+-----------+--------+--------+
So, if 1005 Extension DIALS 908 (no more digits), that call should be blocked
according to the Action field. if 1005 Extension Dials 3025555555 (no more
digits), that Call will be prefixed with number 1.
Iif 1005 Extension Dials 00325698289115 (international), that Call will NOT
me modified according to Rule # 3, since that rule ONLY applies to EXT 2005, so
the CALL will be sent as it was Dialed to the Server.
If that same last Call was made by 2005, the number would MATCH the rule,
since it begins with 00 and Z is ANY DIGIT ANY OCURRENCE. So, the dialed number will be Subtracted "2" digits from the beginning, and then prefixed with "011" before being sent to the trunk.
The important field here I think is ruledigits (and of course extension),
which will be used by PHP for the ARRAY of rules per EXT. then the action will
be read only if the condition was meet.
And the condition something like this, although this one only represents just
1 rule and case:
if(preg_match("/^1$N$X$N(555)$Z/", $number))
{
// $number format matches
echo "yes"; // apply the action according the table
}
else
{
echo "no"; // no action applied, proceed with the call.
}
I would need condition(s) that must be created on the fly as soon as the actual
Call (requesting the access to the database through a php script) finds that
there are not just 1, but some rules created for THAT extension individually.
How can I make or design a General function that can Access the database "rules"
table, and ARRAY the group of all the conditions created under the caller
extension in order to apply them to the actual call?
Note: The field
"ruledigits" on the table "rules" will only accept the following characters:
N, Z, *, #, or Any number.
Here's how I would personally approach using MySQL data to build Regex patterns for checking data:
//Simplified array
$dbResults = array(
array(
'id' => 1,
'extension' => 1005,
'order' => 1,
'description' => 'rule 1',
'ruledigits' => '9XX',
'prefix' => null,
'ruletype' => 'Block'
),
array(
'id' => 2,
'extension' => 1005,
'order' => 2,
'description' => 'rule 2',
'ruledigits' => '302NXXXXXX',
'prefix' => 1,
'ruletype' => 'Mod'
),
array(
'id' => 3,
'extension' => 2005,
'order' => 3,
'description' => 'rule 3',
'ruledigits' => '00Z',
'prefix' => '001',
'ruletype' => 'Mod'
)
);
$regexParts = array(
'N' => '[2-9]'
,'Z' => '\d*'
,'X' => '\d'
,'x' => '\d'
);
//Static test vars
$userExt = 1005;
$dialTests = array('00325698289115','908','3025555555');
echo 'Testing user extension: '.$userExt;
echo '<br /><br />';
//This loop is for testing purposes only, the contents are all the live system would use
foreach($dialTests as $testNo)
{
$actionTaken = 'None';
//By default, if nothing happens, make sure the final number is the original one we saw
$finalNo = $testNo;
foreach($dbResults as $row)
{
if($userExt != $row['extension']) continue;//If it's not the right extension, skip the rest of this loop iteration's code and move on to the next row
$regex = '';
$tokens = str_split($row['ruledigits']);//Turn the string into an array, so we can parse each character individually
foreach($tokens as $token)
{
if(isset($regexParts[$token])) $regex .= $regexParts[$token];//If the letter has a special meaning, use that
else $regex .= $token;//else just throw that exact letter/number in
}
if(preg_match('#^'.$regex.'$#',$testNo)>0)
{
$actionTaken = $row['ruletype'];//Log what action has been taken
if($actionTaken=='Mod')
{
$finalNo = $row['prefix'].$testNo;//Do the mod action
}
else if($actionTaken=='Block')
{
$finalNo = false;//Nullify the final number, so we know to block the call later on
}
}
}
//Here we just dump some info for testing purposes
echo $testNo.': Action taken = '.$actionTaken;
if($actionTaken=='Block') echo ' - Call terminated.';
if($actionTaken=='Mod') echo ' - New number = '.$finalNo;
echo '<hr />';
}
View the output demo on phpfiddle (click Run/F9)
You can use asterisk realtime architecture and correct view to match extensions table.
Or you can use mysql query with REGEXP/RLIKE construction.
https://dev.mysql.com/doc/refman/5.1/en/regexp.html
You can easy change asterisk regexp to general regexp by using simple rewrites like
'.' -> '.*'
'X' -> '[0-9]'
etc

Inserting multiple arrays into different rows

I'm collecting input fields as an array to be inserted into the different rows in the database. However it only inserts the first row of the array.
Please kindly assist. I have some issues handling arrays. I don't know how to construct the query and I've tried to search online and I couldn't get enough help. I'll really appreciate help with the query.
Here is my HTML code:
<input type = "text" class = "form_element" name = "wat_office_type[]" />
<input type = "number" name = "wat_office_price[]" class = "form_element" />
while I use jQuery to add more input boxes.
Here is my php:
$wat_office_type_post = $_POST['wat_office_type'];
$wat_office_price_post = $_POST['wat_office_price'];
$wat_office_type = array();
$wat_office_price = array();
foreach ($wat_office_type_post as $type) {
if (!empty($type))
$wat_office_type[] = $afrisoft->antiHacking($type);
}
foreach ($wat_office_price_post as $post) {
if (!empty($post))
$wat_office_price[] = $afrisoft->antiHacking($post);
}
I want to insert into 2 separate rows and achieve something like this:
--------------------------------------------
| Pk | wat_office_type | wat_office_price |
--------------------------------------------
| 1 | executive office | 1000 |
--------------------------------------------
| 2 | Training room | 4000 |
--------------------------------------------
| 3 | Events room | 5000 |
--------------------------------------------
I'll kindly appreciate if I can get help with the insert query (mysql,php) on how I can insert all values of the first array in one column, and values of the second array into the second column, while each are matching d numbers of arrays supplied.
Thanks.
If I understand correctly, you need to combine the two arrays into something like
$list = array(
array('pk' => 1, 'type' => 'executive', 'price' => 1000),
array('pk' => 2, 'type' => 'training room', 'price' => 4000),
array('pk' => 3, 'type' => 'events room', 'price' => 5000)
);
then you can call a foreach loop to query the database with each element, like
foreach($list as $key => $value) {
$sql = "INSERT INTO table_name (pk, type, price) VALUES (?, ?, ?)";
$query = $this->db->prepare($sql);
$params = array($value['pk'], $value['type'], $value['price']);
if($query->execute($params)) {
return true;
} else {
return false;
}
}
so instead doing two foreach loops and putting results in different arrays, you could use one foreach loop and create an associative array for each set of results (I assume the count on both of your arrays is the same?) as I mentioned above.

Categories