I have this array
Array (
[3] => Array (
[IDFattura] => 3
[Data_Scadenza] => 2011-06-23
[Importo] => 343.30
[IDTipo_Offerta] => A
[Email] => stefano#email.it )
[4] => Array (
[IDFattura] => 4
[Data_Scadenza] => 2011-06-23
[Importo] => 98.40
[IDTipo_Offerta] => A
[Email] => stefano#email.it )
[7] => Array (
[IDFattura] => 33
[Data_Scadenza] => 2011-06-23
[Importo] => 18.40
[IDTipo_Offerta] => A
[Email] => tom#email.it ) )
Now I need send ONE email to each Email, but stefano#email.it (in email body ) will have a table with two rows, instead of Tom that will have 1 row.
Hope you understand me!
try this code
$newarray = array();
foreach($array as $item) $newarray[$item["Email"]] = 1;
$sendarray = array_keys($newarray);
foreach($sendarray as $item) mail(...);
you should also consider array_unique
good luck
You should reformat your array like this:
$newArray = array();
foreach ($yourArray as $key => $value) {
$newArray[$value['Email']][] = $value;
}
It returns array grouped by Email. And for stefano#email.it tou will have an array with 2 items.
Loop through the array and group invoices by email:
$invoicesByEmail = array();
foreach($invoices as $invoice) {
if(!isset($invoicesByEmail[$invoice['Email']])) {
$invoicesByEmail[$invoice['Email']] = array();
}
$invoicesByEmail[$invoice['Email']][] = $invoice;
}
Then, it's a matter of looping through the grouped invoice and mailing them.
foreach($invoicesByEmail as $recipient => $invoices) {
$emailBody = '';
foreach($invoices as $invoice) {
// Parse your invoice
}
Mailer::send($recipient, $emailBody, $headers);
}
Personally, I'd structure the array slightly different. Instead of having numeric keys, i'd set the key as the email address. This way you can simply use array_unique.
If you can't change the array as you get it now, you can loop through it and extract each email address out and insert it into a new array:
$uniqueEmails = array();
foreach ($yourArray as $k => $v) {
if (isset($v['Email']) $uniqueEmails[$v['Email']] = $v['Email'];
}
return $uniqueEmails;
Related
I am having a terrible time getting this to work I have been struggling with it for a couple hours now. Can someone please help me? I have included a fiddle.
I believe my problem is in this string:
$$salesAndOwner[$i]["$salesAndOwner[$i]".$l] = $salesAndOwner[$i.$l][$param] = $values[$l];
Basically I have the following multidimensional array:
[sales] => Array
(
[FirstName] => Array
(
[0] => salesFirst1
[1] => salesFirst2
)
[LastName] => Array
(
[0] => salesLast1
[1] => salesLast2
)
)
[decisionmaker] => Array
(
[FirstName] => Array
(
[0] => dmFirst1
[1] => dmFirst2
)
[LastName] => Array
(
[0] => dmLast1
[1] => dmLast2
)
)
)
I need this to be reorganized like I did with the following array:
Array
(
[additionallocations0] => Array
(
[Address] => Address1
[State] => State1
)
[additionallocations1] => Array
(
[Address] => Address2
[State] => State2
)
)
Here is the original:
Array
(
[additionallocations] => Array
(
[Address] => Array
(
[0] => Address1
[1] => Address2
)
[State] => Array
(
[0] => State1
[1] => State2
)
)
This is how I reorganize the above array:
if(isset($_POST['additionallocations'])) {
$qty = count($_POST['additionallocations']["Address"]);
for ($l=0; $l<$qty; $l++)
{
foreach($_POST['additionallocations'] as $param => $values)
{
$additional['additionallocations'.$l][$param] = $values[$l];
}
}
And this is what I am using for the sales and decisionmaker array. If you notice I have an array that contains sales and decisionmaker in it. I would like to be able to sort any future arrays by just adding its primary arrays name. I feel I am close to solving my problem but I can not get it to produce right.
$salesAndOwner = array(0 => "sales", 1 => "decisionmaker");
for($i = 0; $i < 2; $i++){
$qty = count($_POST[$salesAndOwner[$i]]["FirstName"]);
for ($l=0; $l<$qty; $l++)
{
foreach($_POST[$salesAndOwner[$i]] as $param => $values)
{
$$salesAndOwner[$i]["$salesAndOwner[$i]".$l] = $salesAndOwner[$i.$l][$param] = $values[$l];
}
}
}
In the above code I hard coded 'sales' into the variable I need it to make a variable name dynamically that contains the sales0 decisionmaker0 and sales1 decisionmaker1 arrays so $sales and $decisionmaker
I hope this makes sense please let me know if you need any more info
Let's break it down. Using friendly variable names and spacing will make your code a lot easier to read.
Remember. The syntax is for you to read and understand easily. (Not even just you, but maybe future developers after you!)
So you have an array of groups. Each group contains an array of attributes. Each attribute row contains a number of attribute values.
PHP's foreach is a fantastic way to iterate through this, because you will need to iterate through (and use) the index names of the arrays:
<?php
$new_array = array();
// For each group:
foreach($original_array as $group_name => $group) {
// $group_name = e.g 'sales'
// For each attribute in this group:
foreach($group as $attribute_name => $attributes) {
// $attribute_name = e.g. 'FirstName'
// For each attribute value in this attribute set.
foreach($attributes as $row_number => $attribute) {
// E.g. sales0
$row_key = $group_name . $row_number;
// if this is the first iteration, we need to declare the array.
if(!isset($new_array[$row_key])) {
$new_array[$row_key] = array();
}
// e.g. Array[sales0][FirstName]
$new_array[$row_key][$attribute_name] = $attribute;
}
}
}
?>
With this said, this sort of conversion may cause unexpected results without sufficient validation.
Make sure the input array is valid (e.g. each attribute group has the same number of rows per group) and you should be okay.
$salesAndOwner = array("sales", "decisionmaker");
$result = array();
foreach ($salesAndOwner as $key) {
$group = $_POST[$key];
$subkeys = array_keys($group);
$first_key = $subkeys[0];
foreach ($group[$first_key] as $i => $val) {
$prefix = $key . $i;
foreach ($subkeys as $subkey) {
if (!isset($result[$prefix])) {
$result[$prefix] = array();
}
$result[$prefix][$subkey] = $val;
}
}
}
DEMO
Try
$result =array();
foreach($arr as $key=>$val){
foreach($val as $key1=>$val1){
foreach($val1 as $key2=>$val2){
$result[$key.$key2][$key1] = $val2;
}
}
}
See demo here
I have arrays $devices,$port
print_r($devices);
Array(
[0] => cisco1
[1] => cisco2
)
print_r($port);
Array
(
[0] => Port1/1/1
[1] => Port1/1/10
[2] => Port1/1/11
)
I want to create an array $devlist which would be something like this:
Array(
[cisco1] =>Port1/1/1
Port1/1/10
Port1/1/11
[cisco2] =>Port2/1/1
Port2/1/10
Port2/1/11
)
My point is there is an array of devices($devices) and arrays of ports that are there in each of the device.
The $port array gets created newly for each device in the $device array.
What i have tried so far:
foreach ($devices as $value)
{
$port=();
//iam polling the respective device and getting a list of ports available for that device in array **$port**
array_push($devices[$value], $port);
}
This method generates an error "array_push() expects parameter 1 to be array, null given"
Kindly excuse me if this seems an easy question becoz iam new to php and scripting as well:(
Do you want something like this? If so, i don't understand why, when you could just use the values from $ports for each $device?
$devices = array
(
'cisco1', 'cisco2'
);
$ports = array
(
'Port1/1/1',
'Port1/1/10',
'Port1/1/11'
);
$dev_list = array();
foreach ($devices as $device)
{
$dev_list[$device] = array();
foreach ($ports as $port)
{
array_push($dev_list[$device], $port);
}
}
echo '<pre>';
print_r($dev_list);
echo '</pre>';
Array
(
[cisco1] => Array
(
[0] => Port1/1/1
[1] => Port1/1/10
[2] => Port1/1/11
)
[cisco2] => Array
(
[0] => Port1/1/1
[1] => Port1/1/10
[2] => Port1/1/11
)
)
Try this:
$devlist = array();
foreach ($devices as $value)
{
$port=();
//iam polling the respective device and getting a list of ports available for that device in array **$port**
$devlist[$value] = $port;
}
You may use a foreach or a for loop but the main thing is that you have to use variable variables.
Solution One:
$devlist = array();
foreach($devices as $key => $device){
$devlist[$device] = ${"port".($key+1)};
}
Solution Two:
$devlist = array();
$size = sizeof($devices);
for($i = 0; $i < $size; $i++){
$devlist[$devices[$i]] = ${"port".($i+1)};
}
If I understand your question, you want something like this:
$arr = array
(
'cisco1' => array
(
'Port1/1/1',
'Port1/1/10',
'Port1/1/11'
),
'cisco2' => array
(
'Port1/1/1',
'Port1/1/10',
'Port1/1/11'
)
);
foreach($arr as $key => $value) // use the foreach-loop like this to get both keys and values
{
echo "$key: <br />-" . implode('<br />-', $value) . '<br /><br />';
}
prints:
cisco1:
-Port1/1/1
-Port1/1/10
-Port1/1/11
cisco2:
-Port1/1/1
-Port1/1/10
-Port1/1/11
I have two multidimensional arrays. First one $properties contains english names and their values. My second array contains the translations. An example
$properties[] = array(array("Floor"=>"5qm"));
$properties[] = array(array("Height"=>"10m"));
$translations[] = array(array("Floor"=>"Boden"));
$translations[] = array(array("Height"=>"Höhe"));
(They are multidimensional because the contains more elements, but they shouldn't matter now)
Now I want to translate this Array, so that I its at the end like this:
$properties[] = array(array("Boden"=>"5qm"));
$properties[] = array(array("Höhe"=>"10m"));
I have managed to build the foreach construct to loop through these arrays, but at the end it is not translated, the problem is, how I tell the array to replace the key with the value.
What I have done is this:
//Translate Array
foreach ($properties as $PropertyArray) {
//need second foreach because multidimensional array
foreach ($PropertyArray as $P_KiviPropertyNameKey => $P_PropertyValue) {
foreach ($translations as $TranslationArray) {
//same as above
foreach ($TranslationArray as $T_KiviTranslationPropertyKey => $T_KiviTranslationValue) {
if ($P_KiviPropertyNameKey == $T_KiviTranslationPropertyKey) {
//Name found, save new array key
$P_KiviPropertyNameKey = $T_KiviTranslationValue;
}
}
}
}
}
The problem is with the line where to save the new key:
$P_KiviPropertyNameKey = $T_KiviTranslationValue;
I know this part is executed correctly and contains the correct variables, but I believe this is the false way to assing the new key.
This is the way it should be done:
$properties[$oldkey] = $translations[$newkey];
So I tried this one:
$PropertyArray[$P_KiviPropertyNameKey] = $TranslationArray[$T_KiviTranslationPropertyKey];
As far as I understood, the above line should change the P_KiviPropertyNameKey of the PropertyArray into the value of Translation Array but I do not receive any error nor is the name translated. How should this be done correctly?
Thank you for any help!
Additional info
This is a live example of the properties array
Array
(
[0] => Array
(
[country_id] => 4402
)
[1] => Array
(
[iv_person_phone] => 03-11
)
[2] => Array
(
[companyperson_lastname] => Kallio
)
[3] => Array
(
[rc_lot_area_m2] => 2412.7
)
[56] => Array
(
[floors] => 3
)
[57] => Array
(
[total_area_m2] => 97.0
)
[58] => Array
(
[igglo_silentsale_realty_flag] => false
)
[59] => Array
(
[possession_partition_flag] => false
)
[60] => Array
(
[charges_parkingspace] => 10
)
[61] => Array
(
[0] => Array
(
[image_realtyimagetype_id] => yleiskuva
)
[1] => Array
(
[image_itemimagetype_name] => kivirealty-original
)
[2] => Array
(
[image_desc] => makuuhuone
)
)
)
And this is a live example of the translations array
Array
(
[0] => Array
(
[addr_region_area_id] => Maakunta
[group] => Kohde
)
[1] => Array
(
[addr_town_area] => Kunta
[group] => Kohde
)
[2] => Array
(
[arable_no_flag] => Ei peltoa
[group] => Kohde
)
[3] => Array
(
[arableland] => Pellon kuvaus
[group] => Kohde
)
)
I can build the translations array in another way. I did this like this, because in the second step I have to check, which group the keys belong to...
Try this :
$properties = array();
$translations = array();
$properties[] = array("Floor"=>"5qm");
$properties[] = array("Height"=>"10m");
$translations[] = array("Floor"=>"Boden");
$translations[] = array("Height"=>"Höhe");
$temp = call_user_func_array('array_merge_recursive', $translations);
$result = array();
foreach($properties as $key=>$val){
foreach($val as $k=>$v){
$result[$key][$temp[$k]] = $v;
}
}
echo "<pre>";
print_r($result);
output:
Array
(
[0] => Array
(
[Boden] => 5qm
)
[1] => Array
(
[Höhe] => 10m
)
)
Please note : I changed the array to $properties[] = array("Floor"=>"5qm");, Removed a level of array, I guess this is how you need to structure your array.
According to the structure of $properties and $translations, you somehow know how these are connected. It's a bit vague how the indices of the array match eachother, meaning the values in $properties at index 0 is the equivalent for the translation in $translations at index 0.
I'm just wondering why the $translations array need to have the same structure (in nesting) as the $properties array. To my opinion the word Height can only mean Höhe in German. Representing it as an array would suggest there are multiple translations possible.
So if you could narrow down the $translations array to an one dimensional array as in:
$translation = array(
"Height"=>"Höhe",
"Floor"=>"Boden"
);
A possible loop would be
$result = array();
foreach($properties as $i => $array2) {
foreach($array2 as $i2 => $array3) {
foreach($array3 as $key => $value) {
$translatedKey = array_key_exists($key, $translations) ?
$translations[$key]:
$key;
$result[$i][$i2][$translatedKey] = $value;
}
}
}
(I see every body posting 2 loops, it's an array,array,array structure, not array,array ..)
If you cannot narrow down the translation array to a one dimensional array, then I'm just wondering if each index in the $properties array matches the same index in the $translations array, if so it's the same trick by adding the indices (location):
$translatedKey = $translations[$i][$i2][$key];
I've used array_key_exists because I'm not sure a translation key is always present. You have to create the logic for each case scenario yourself on what to check or not.
This is a fully recursive way to do it.
/* input */
$properties[] = array(array("Floor"=>"5qm", array("Test"=>"123")));
$properties[] = array(array("Height"=>"10m"));
$translations[] = array(array("Floor"=>"Boden", array("Test"=>"Foo")));
$translations[] = array(array("Height"=>"Höhe"));
function array_flip_recursive($arr) {
foreach ($arr as $key => $val) {
if (is_array($val)) {
$arr[$key] = array_flip_recursive($val);
}
else {
$arr = #array_flip($arr);
}
}
return $arr;
}
function array_merge_it($arr) {
foreach ($arr as $key => $val) {
if (is_array($val)) {
$arr[$key] = array_merge_it($val);
} else {
if(isset($arr[$key]) && !empty($arr[$key])) {
#$arr[$key] = $arr[$val];
}
}
}
return $arr;
}
function array_delete_empty($arr) {
foreach ($arr as $key => $val) {
if (is_array($val)) {
$arr[$key] = array_delete_empty($val);
}
else {
if(empty($arr[$key])) {
unset($arr[$key]);
}
}
}
return $arr;
}
$arr = array_replace_recursive($properties, $translations);
$arr = array_flip_recursive($arr);
$arr = array_replace_recursive($arr, $properties);
$arr = array_merge_it($arr);
$arr = array_delete_empty($arr);
print_r($arr);
http://sandbox.onlinephpfunctions.com/code/d2f92605b609b9739964ece9a4d8f389be4a7b81
You have to do the for loop in this way. If i understood you right (i.e) in associative array first key is same (some index).
foreach($properties as $key => $values) {
foreach($values as $key1 => $value1) {
$propertyResult[] = array($translations[$key][$key1][$value1] => $properties[$key][$key1][$value1]);
}
}
print_r($propertyResult);
I've got the following array stored in a $_SESSION
[Bookings] => Array
(
[date] => Array
(
[0] => 1/12/2013
[1] => 1/19/2013
[2] => 2/03/2013
)
[price] => Array
(
[0] => 100
[1] => 150
[2] => 120
)
)
However I want to use a foreach loop and perform calculation on both values within the array.I can't seem to fugure out how I can use the foreach to accomodate multivalues, I've got a sample of a foreach I wrote below of what I'm trying to achieve. Anyone point me in the right direction.
foreach ($_SESSION['Bookings'] as $bookings)
{
myDate = $bookings[date];
myPrice = $bookings[price];
// Some other stuff here
}
foreach ($_SESSION['Bookings']['date'] as $key => $value) {
$myDate = $value;
$myPrice = $_SESSION['Bookings']['price'][$key];
}
simpler I guess :)
foreach (array_keys($_SESSION['Bookings']['date']) as $key)
{
$myDate = $_SESSION['Bookings']['date'][$key];
$myPrice = $_SESSION['Bookings']['price'][$key];
}
Should work?
Some info on: array_keys
just loop through on of your subarrays and read the corresponding value from the other
foreach ( $_SESSION['Bookings'][ 'date' ] as $key => $myDate) {
$myPrice = $_SESSION['Bookings'][ 'price' ][ $key ];
// here you can access to $myDate and $myPrice
// Some other stuff here
}
Hello I have an array that looks like this,
Array
(
[cfi_title] => Mr
[cfi_firstname] => Firstname
[cfi_surname] => Lastname
[cfi_email] => test#test.co.uk
[cfi_subscribe_promotional] =>
[cfi_tnc] =>
[friendsName] => Array
(
[0] => Firstname 1
[1] => Firstname 2
[2] => Firstname 3
)
[friendsEmail] => Array
(
[0] => email1#address.com
[1] => email2#address.com
[2] => email3#address.com
)
[submit_form] => Submit
)
My dilema is I need to save the values from the friendsName and friendsEmail arrays into a database, I know I can loop through them but how can I send the matching data, for example I need to save [friendsName][0] and friendsEmail][0] on the same row of database?
I know I need to use a foreach but I just cannot figure out the logic.
foreach($friendsName as $key=>$val) {
$friend = $val;
$email = friendsEmail[$key];
}
or
$count = count($friendsName);
for($i = 0; $i< $count; ++$i) {
$friend = $friendsName[$i];
$email = $friendsEmail[$i];
}
Each of the above examples are using the assumption that the array key is the matching identifier between the two bits of data
Complete solution
//Prepare an array for the collected data
$data = array();
//Loop through each of your friends names
foreach($array['friendsName'] as $key => $value)
{
//Save the name as part of an associative array, using the key as an identifier
$data[$key] = array("name" => $value);
}
//Loop through the emails
foreach($array['friendsEmail'] as $key => $value)
{
//The array is allready there so just save the email
$data[$key]['email'] = $value;
}
$datanow contains your values paired up.