I've got an array like this:
$sendemail= array(
'apple#yahoo.com' => '123456781234567',
'apple#yahoo.com' => '013881002296968',
'cherry#yahoo.com' => '3553220865206561',
'orange#yahoo.com' => '358805051217453',
'apple#yahoo.com' => '357998054217777',
'cherry#yahoo.com' => '013881002296968',
);
I would like to have an output like this:
'apple#yahoo.com' => 123456781234567, 013881002296968, 357998054217777
'cherry#yahoo.com' => 3553220865206561, 013881002296968
'orange#yahoo.com' => 358805051217453
to be able to use the keys as email address and the values as my email's buddy
$email= 'apple#yahoo.com';
$body= '123456781234567, 013881002296968, 357998054217777';
mail($email, 'Your codes', $body);
And the same for the other email addresses.
PLEASE NOTE that 2 keys might have the same values which is fine (e.g. apple#yahoo.com and cherry#yahoo.com have the same values; the value will be sent to both of them)
I used this 'for loop' but didn't work. first of all I cant group them based on email addresses, second of all the same values will not be assigned to the other email addresses; like '013881002296968' which should be shared with apple#yahoo.com and cherry#yahoo.com
$sendmail= array(
'123456781234567' => 'apple#yahoo.com',
'013881002296968' => 'apple#yahoo.com',
'3553220865206561' => 'cherry#yahoo.com',
'358805051217453' => 'orange#yahoo.com',
'357998054217777' => 'apple#yahoo.com',
'013881002296968' => 'cherry#yahoo.com',
);
$out = array();
foreach($sendmail as $key=>$value)
if(array_key_exists($value, $out)) {
$out[$value][] = $key;
}
else {
$out[$value] = array($key);
}
Output
array (
'apple#yahoo.com' =>
array (
0 => 123456781234567,
1 => 013881002296968,
2 => 357998054217777,
),
'cherry#yahoo.com' =>
array (
0 => 3553220865206561,
),
'orange#yahoo.com' =>
array (
0 => 358805051217453,
),
)
Here is an alternative to Kris' method that has less iterations, but more conditionals:
Input:
$sendmail= array(
'123456781234567' => 'apple#yahoo.com',
'013881002296968' => 'apple#yahoo.com',
'3553220865206561' => 'cherry#yahoo.com',
'358805051217453' => 'orange#yahoo.com',
'357998054217777' => 'apple#yahoo.com',
'013881002296968' => 'cherry#yahoo.com',
);
Method:
foreach($sendmail as $k=>$v){
if(!isset($out[$v])){$out[$v]='';} // initialize new element with empty string
$out[$v].=($out[$v]?',':'').$k; // concat the values with conditional comma
}
var_export($out);
Output:
array (
'apple#yahoo.com' => '123456781234567,357998054217777',
'cherry#yahoo.com' => '013881002296968,3553220865206561',
'orange#yahoo.com' => '358805051217453',
)
Use implode on the element of your result array. live demo
foreach($sendmail as $key=>$value) {
$out[$value][] = $key;
}
$out = array_map(function($v){return implode(',', $v);}, $out);
Related
What I'm trying to accomplish is to write a html string in a controller with array values being looped in it. So for example;
$content = "Your store, at location A, has these items added to them". add array loop here. "Do take note!";
My array would be as such
array (
0 =>
array (
'id' => '5db29b6d31c391731239bbdf',
'name' => 'Diamond bracelet (sample)',
'tags' =>
array (
0 => 'female',
1 => 'jewelry',
),
'category' => 'Accessories',
'sku' => '1029EHW',
'priceType' => 'Fixed',
'unitPrice' => 190,
'cost' => 90,
'trackStockLevel' => true,
'isParentProduct' => false,
),
1 =>
array (
'id' => '5db29b6d31c391731239bbdb',
'name' => 'Long-sleeved shirt(sample)(M)',
'tags' =>
array (
0 => 'tops',
1 => 'cotton',
),
'category' => 'Women\'s Apparel',
'sku' => 'ABC1234-M',
'priceType' => 'Fixed',
'unitPrice' => 47.170000000000002,
'cost' => 20,
'trackStockLevel' => true,
'isParentProduct' => false,
'parentProductId' => '5db29b6d31c391731239bbd4',
'variationValues' =>
array (
0 =>
array (
'variantGroupId' => '5db29b6d31c391731239bbd5',
'value' => 'M',
),
),
),
)
note that the array can have many instances of product_name and sku, or can have none.
How do i populate this in my string to be like;
$content = "Your store, at location A, has these items added to them, 1) asd, 2)def, 3)asf . Do take note!
Try this, I've used \sprintf for ease, check if the output serves your purpose.
<?php
function stringMethod(): string
{
$count = 0;
$arrayString = [];
$array = [['product_name' => 'abc', 'product_sku' => 'def'],['product_name' => 'abc', 'product_sku' => 'asd']];
foreach ($array as $value){
$count++;
$arrayString[] = sprintf('%s)%s', $count, $value['product_sku']);
}
$string = \implode(',', $arrayString);
return \sprintf("Your store, at location A, has these items added to them %s Do take note!", $string);
}
echo stringMethod();
Hope this will help you. try to do it in less number of lines
$content = "Your store, at location A, has these items added to them, ";
$productArray = array (0 => array ('id' => '5db29b6d31c391731239bbdf','name' => 'Diamond bracelet (sample)','tags' => array (0 => 'female',1 => 'jewelry',),'category' => 'Accessories','sku' => '1029EHW','priceType' => 'Fixed','unitPrice' => 190,'cost' => 90,'trackStockLevel' => true,'isParentProduct' => false,),1 => array ('id' => '5db29b6d31c391731239bbdb','name' => 'Long-sleeved shirt(sample)(M)','tags' => array (0 => 'tops',1 => 'cotton',),'category' => 'Women\'s Apparel','sku' => 'ABC1234-M','priceType' => 'Fixed','unitPrice' => 47.170000000000002,'cost' => 20,'trackStockLevel' => true,'isParentProduct' => false,'parentProductId' => '5db29b6d31c391731239bbd4','variationValues' => array (0 => array ('variantGroupId' => '5db29b6d31c391731239bbd5','value' => 'M'))));
foreach ($productArray as $key => $product) {
$content .= ($key+1).') '.$product['name'];
if (count($productArray)-1!=$key) {
$content .= ', ';
}
}
$content .= ". Do take note!";
$content = "Your store, at location A, has these items added to them,". $this->getItemList($collection) .". Do take note!"
# Somewhere else in the controller
protected function getItemList(Collection $collection): string
{
return $collection->pluck('name')
->merge($collection->pluck('sku'))
->map(function($item, $key) {
return ($key + 1) . ') ' . $item;
})
->implode(', ');
}
An easy solution would be
$content = "Your store, at location A, has these items added to them";
$array = array(0=>["product_name" => "abc", "product_sku" => "def"], 1=>['product_name' => 'kdkf', 'product_sku'=> 'ljbkj']);
for($i = 0; $i<count($array); $i++){
$content .= ($i+1).") ".$array[$i]['product_name].' '.$array[$i]['product_sku'].', ';
}
$content .= "Do take note.";
This way you're just constantly concatonating the string value in separate parts instead of trying to inject it in the middle of the parent string.
not tested, may be syntax errors
How can I make the values of array comma separated values?
this is my array:
array(
'product_name' =>
0 => 'pn1',
1 => 'pn2'
'supply_product_name' =>
0 => 'ps1'
'custom_product_code' =>
0 => string 'cpc1'
)
how to achieve to look like these:
array(
'product_name' => 'pn1, pn2' ,
'supply_product_name' => 'ps1' ,
'custom_product_code' => 'cpc1'
)
Try these code:
$arr = array(
'product_name' => ['pn1', 'pn2'],
'supply_product_name' => ['ps1'],
'custom_product_code' => ['cpc1']
);
foreach($arr as $key => $val)
{
$arr[$key] = implode($val, ',');
}
var_dump($arr);
I have a following array,
$versions = array
(
'0.9.md5' => '/var/www/md5_test/0.9.md5',
'1.0.0.md5' => '/var/www/md5_test/1.0.0.md5',
'1.0.1.md5' => '/var/www/md5_test/1.0.1.md5',
'1.0.2.md5' => '/var/www/md5_test/1.0.2.md5',
'1.0.3.md5' => '/var/www/md5_test/1.0.3.md5',
'1.0.9.1.md5' => '/var/www/md5_test/1.0.9.1.md5',
'1.0.9.10.1.md5' => '/var/www/md5_test/1.0.9.10.1.md5',
'1.0.9.10.md5' => '/var/www/md5_test/1.0.9.10.md5',
'1.1.3.md5' => '/var/www/md5_test/1.1.3.md5',
'1.0.9.2.md5' => '/var/www/md5_test/1.0.9.2.md5',
'1.0.9.3.md5' => '/var/www/md5_test/1.0.9.3.md5',
'1.0.9.8.md5' => '/var/www/md5_test/1.0.9.8.md5',
'1.0.9.9.1.md5' => '/var/www/md5_test/1.0.9.9.1.md5',
'1.0.9.9.md5' => '/var/www/md5_test/1.0.9.9.md5',
'1.0.9.md5' => '/var/www/md5_test/1.0.9.md5',
'1.1.0.md5' => '/var/www/md5_test/1.1.0.md5',
'1.1.1.md5' => '/var/www/md5_test/1.1.1.md5',
'1.1.2.md5' => '/var/www/md5_test/1.1.2.md5',
);
In this array i want to sort this by keys. I have searched,
Ex: It should order like: 1.0.9.md5, 1.0.9.1.md5,.. , 1.0.9.10.md5, 1.0.9.10.1.md5
I have tried
ksort($versions);
But i could't get exactly what i want.
If these are version numbers, and you need to sort by the version so that 1.0.9.2.md5 comes before 1.0.9.10.1.md5 then you need a custom sort based on semantic versioning:
uksort($versions, 'version_compare');
Demo
Remove the ".md5" -> ksort() -> add the ".md5" again.
foreach($versions as $key => $value) {
$newKey = str_replace(".md5", "", $key);
$new[$newKey] = $value;
}
ksort($new);
foreach($new as $key => $value) {
$newKey = $key . ".md5";
$result[$newKey]= $value;
}
print_r($result);
Result:
Array
(
[0.9.md5] => /var/www/md5_test/0.9.md5
[1.0.0.md5] => /var/www/md5_test/1.0.0.md5
[1.0.1.md5] => /var/www/md5_test/1.0.1.md5
[1.0.2.md5] => /var/www/md5_test/1.0.2.md5
[1.0.3.md5] => /var/www/md5_test/1.0.3.md5
[1.0.9.md5] => /var/www/md5_test/1.0.9.md5
[1.0.9.1.md5] => /var/www/md5_test/1.0.9.1.md5
[1.0.9.10.md5] => /var/www/md5_test/1.0.9.10.md5
[1.0.9.10.1.md5] => /var/www/md5_test/1.0.9.10.1.md5
[1.0.9.2.md5] => /var/www/md5_test/1.0.9.2.md5
[1.0.9.3.md5] => /var/www/md5_test/1.0.9.3.md5
[1.0.9.8.md5] => /var/www/md5_test/1.0.9.8.md5
[1.0.9.9.md5] => /var/www/md5_test/1.0.9.9.md5
[1.0.9.9.1.md5] => /var/www/md5_test/1.0.9.9.1.md5
[1.1.0.md5] => /var/www/md5_test/1.1.0.md5
[1.1.1.md5] => /var/www/md5_test/1.1.1.md5
[1.1.2.md5] => /var/www/md5_test/1.1.2.md5
[1.1.3.md5] => /var/www/md5_test/1.1.3.md5
)
I want to compare a value of data to a list of element which I had retrieved from php array(decoded json).
First,
This is the first array:
Array1
(
[0] => Array
(
[member_id] => 3
[member_card_num] => 2013011192330791
[member_barcode] => 2300067628912
)
[1] => Array
(
[member_id] => 4
[member_card_num] => 2328482492740000
[member_barcode] => 3545637000
)
[2] => Array
(
[member_id] => 2
[member_card_num] => 40001974318
[member_barcode] => 486126
)
[3] => Array
(
[member_id] => 1
[member_card_num] => 91001310000057698
[member_barcode] => 000057698
)
)
This is the second Array:
Array2
(
[0] => Array
(
[member_id] => 2
[member_card_num] => 40001974318
[member_barcode] => 486126
)
)
Second,
I had retrieved the (member_barcode) which I required.
Here is the code:
For Array1:
foreach ($decode1 as $d){
$merchant_barcode = $d ['member_barcode'];
echo $merchant_barcode;
}
For Array2:
foreach ($decode2 as $d2){
$user_barcode = $d2 ['member_barcode'];
echo $user_barcode;
}
Then,
I get this output():
For Array1(merchant_barcode):
2300067628912
3545637000
486126
000057698
For Array2(user_barcode):
486126
The question is, I would to check and compare whether the user_barcode in Array2(486126) is exist/match to one of the merchant_barcode in Array1.
This is my code,
but it only compare the user_barcode in Array2 to the last element(000057698) in Array1,
I want it to loop through each n check one by one. How can I do that?
public function actionCompareBarcode($user_barcode, $merchant_barcode){
if(($user_barcode) == ($merchant_barcode)){
echo "barcode exist. ";
}
else{
echo "barcode not exist";
}
}
In this case, the output I get is "barcode not exist", but it should be "barcode exist".
Anyone can help? Appreciate that. Im kinda new to php.
You could use a nested loop like:
foreach ($decode2 as $d2)
{
$user_barcode = $d2 ['member_barcode'];
foreach ($decode1 as $d)
{
$merchant_barcode = $d ['member_barcode'];
if ($merchant_barcode == $user_barcode)
{
echo "Match found!";
}
else
{
echo "No match found!";
}
}
}
<?
$a = array(
array(
'member_id' => 3,
'member_card_num' => '2013011192330791',
'member_barcode' => '2300067628912',
),
array(
'member_id' => 4,
'member_card_num' => '2328482492740000',
'member_barcode' => '3545637000',
),
array(
'member_id' => 2,
'member_card_num' => '40001974318',
'member_barcode' => '486126',
),
array(
'member_id' => 1,
'member_card_num' => '91001310000057698',
'member_barcode' => '000057698',
)
);
$b = array(
array(
'member_id' => 2,
'member_card_num' => '40001974318',
'member_barcode' => '486126',
)
);
array_walk($a, function($item) use($b) {
echo ($b['0']['member_barcode'] == $item['member_barcode'] ? "found" : NULL);
});
?>
I'd use array_uintersect() to calculate if those multidimensional arrays have a common element:
<?php
$a = array(
array(
'member_id' => '3',
'member_card_num' => '2013011192330791',
'member_barcode' => '2300067628912',
),
array(
'member_id' => '2',
'member_card_num' => '40001974318',
'member_barcode' => '486126',
)
);
$b = array(
array(
'member_id' => '2',
'member_card_num' => '40001974318',
'member_barcode' => '486126',
)
);
$match = array_uintersect($a, $b, function($valueA, $valueB) {
return strcasecmp($valueA['member_barcode'], $valueB['member_barcode']);
});
print_r($match);
Try calling that method as you are looping through Array1 and comparing the user_barcode to every value
You can compare two array this way too:
$per_arr = array();
$permissions = array()
foreach ($per_arr as $key => $perms) {
if(isset($permissions[$key]['name'])){
echo $per_arr[$key]['name']; //matched data
}else{
echo $per_arr[$key]['name']; //not matched data
}
}
I am having problem with session array variable
$_SESSION ['roomsInfo_' . $intHId] = $strRTypeArr;
It automatically destroy when land on the other page, by using the <form action=”booking_level.php”>
foreach ( $arrRoomInfo as $arrRoomDetails ) {
// occup details
$AdultNum = ( int ) $arrRoomDetails->AdultNum;
$ChildNum = ( int ) $arrRoomDetails->ChildNum;
// child ages details
$strChAgs = '';
if (property_exists ( $arrRoomDetails, 'ChildAges' )) {
$childAgs = $arrRoomDetails->ChildAges->ChildAge;
$arrChAge = array ();
foreach ( $childAgs as $chAgs ) {
$chAgs = $chAgs->attributes ();
$arrChAge [] = $chAgs ['age'];
}
$strChAgs = implode ( ":", $arrChAge );
}
// set array for all the above details
$strRTypeArr [$rtId] [$i] = array (
'myRoomSq' => $i,
'roomSeq' => $arrRmSeq,
'adults' => $AdultNum,
'child' => $ChildNum,
'childAges' => $strChAgs,
'maxGuests' => $maxGuests,
'maxChild' => $maxChild,
'name' => $rmName,
'HotelRoomTypeId' => $rtId,
'roomId' => $roomId,
'isPublish' => $isPublish,
'Occupancy' => array (
"attributes" => array (
'avrNgtlyRtComm' => $avrNgtlyRtComm,
'avrNightPrice' => $avrNtPr,
'bedding' => $bedding
),
"boardBase" => array (
'bbId' => $arrBrdBsId,
'bbName' => $arrBrdBsName,
'bbPrice' => $arrBrdBsPrice,
'bbPublishPrice' => $arrBrdBspBPrce,
'strBBaseExists' => $strBBaseExists
),
'PriceBreakdown' => $arrDay,
"dblBrDownPrTourico" => $dblBrDownPrTourico,
"dblTtlBrDownPr" => $dblTtlBrDownPr,
'Supplements' => $arrSupp
),
'Discount' => $arrDst,
'cancelPolicy' => $strCnlionHtml,
'arrCanPolicy' => $arrCanPolicy,
'arrChcrCanPolicy' => $arrChcrCanPolicy,
'chcr_cancelPolicy' => $chcrCancellation,
'curncy' => $strCurn
);
$i ++;
}
}
}
}
$_SESSION ['roomsInfo_' . $intHId] = $strRTypeArr;
How can I store in Session Array Variable in a MySQL database ?
Just make sure that you assign the array the way you assign a normal value and then store the array in your database.
$_SESSION['dArray'] = $strRTypeArr;
//for example:
$strRTypeArr[0][0] = array (
'myRoomSq' => 2.5,
'roomSeq' => "test this",
'adults' => true,
'child' => 5,
'childAges' => "test"
);
$strRTypeArr[0][1] = array (
'myRoomSq' => 3.5,
'roomSeq' => "this is another test",
'adults' => false,
'child' => 6,
'childAges' => "test"
);
$_SESSION['dArray'] = $strRTypeArr;
echo "<pre>";
print_r($_SESSION['dArray']);
echo "</pre>";
//Store this $_SESSION['dArray'] in your database
You can also use json_encode($_SESSION['dArray']); to store your array in the database.