PHP: How can i combine 2 different array into one - php

Is there a way to combine 2 array into one array?
My first array shows the amount calculated money per day.
$ArrayBefore[] = $amountOfTransactions_prDayArrayBefore;
Output:
Array (
[0] => Array (
[09/02] => 102.83
[08/02] => 3852.49
[07/02] => 2619.23
[06/02] => 1135.24
[05/02] => 2391.4
[04/02] => 2376.15
[03/02] => 2760.05
[02/02] => 1946.41
)
)
My second array shows the amount transactions per day.
$ArrayTrans[] = $amountOfTransactions_prDayArrayTrans;
Output:
Array (
[0] => Array (
[09/02] => 2
[08/02] => 30
[07/02] => 30
[06/02] => 15
[05/02] => 36
[04/02] => 31
[03/02] => 28
[02/02] => 14
)
)
Is there a way to combine both of them to one array. I want them to look like this, so i can spit them out in a table.
Array (
[0] => Array (
[09/02] => 102.83 => 2
[08/02] => 3852.49 => 30
[07/02] => 2619.23 => 30
[06/02] => 1135.24 => 15
[05/02] => 2391.4 => 36
[04/02] => 2376.15 => 31
[03/02] => 2760.05 => 28
[02/02] => 1946.41 => 14
)
)

i call first array $a , second array $b
foreach ( $a[0] as $key=>$value )
{
$c[0]["$value"] = $b[0][$key];
}
and
$c = array (size=1)
0 =>
array (size=8)
'102.83' => int 2
'3852.49' => int 30
'2619.23' => int 30
'1135.24' => int 15
'2391.4' => int 36
'2376.15' => int 31
'2760.05' => int 28
'1946.41' => int 14

Use array_merge():
$result = array_merge($ArrayBefore, $ArrayTrans);
EDIT:
Understand that my php is rusty, but how about something along the lines of:
foreach($ArrayBefore[] as $key => $value) {
$tmpPair[$value] = $ArrayTrans[0][$key];
$newArray[$key] = $tmpPair;
}
You may also be able to simplify it
foreach($ArrayBefore[] as $key => $value) {
$newArray[$key][$value] = $ArrayTrans[0][$key];
}

Related

array_multisort sort by order of appearance of assisting array

I'd like your help with this tough situation I'm in. Suppose the following array $_xm_anal that holds the sizes/availability info of a product:
Array
(
[availability] => Array
(
[BLK] => Array
(
[SIZE4] => 2
[SIZE8] => 1
[SIZE10] => 3
[SIZE14] => 8
[SIZE16] => 4
[SIZE19] => 12
[SIZE24] => 1
)
)
[color_list] => Array
(
[BLK] => Array
(
[0] => BLK
[COLORCODE] => BLK
[1] => BLACK
[ColorLANG] => BLACK
)
)
[size_list] => Array
(
[ID] => 20
[SIZE1] => 39
[SIZE2] => 40
[SIZE3] => 40.5
[SIZE4] => 41
[SIZE5] => 41.5
[SIZE6] => 42
[SIZE7] => 42.5
[SIZE8] => 43
[SIZE9] => 43.5
[SIZE10] => 44
[SIZE11] => 44.5
[SIZE12] => 45
[SIZE13] => 45.5
[SIZE14] => 46
[SIZE15] => 46.5
[SIZE16] => 47
[SIZE17] => 47.5
[SIZE18] => 48
[SIZE19] => 48.5
[SIZE20] => 49
[SIZE21] => 36
[SIZE22] => 37
[SIZE23] => 37.5
[SIZE24] => 38
[SIZE25] => 38.5
[TitleLANG] => NO NO NO
)
[text_size] => Array
(
[SIZE4] => 41
[SIZE8] => 43
[SIZE10] => 44
[SIZE14] => 46
[SIZE16] => 47
[SIZE19] => 48.5
[SIZE24] => 38
)
)
The PHP code that translates the above array into the appropriate size options is the one below:
foreach ($_xm_anal["availability"][$Prod["color_code"]] as $k => $v) { ?> // $Prod["color_code"] equals to BLK in this example
<? if ($v) {
$hasSize = 1; ?>
<li> <?= $_xm_anal["size_list"][$k] ?></li>
<? } else { ?>
<li style="background:#f0eee8"> <s><?= $_xm_anal["size_list"][$k] ?></s></li>
<? } ?>
<? } ?>
And the final outcome looks like this:
The problem started when they added sizes in an unsorted manner, ie size 38 corresponds to key SIZE24, so $_xm_anal['availability']['BLK'] needs to be sorted in the appropriate order according to its keys, which can be determined by $_xm_anal['text_size']
So I wrote the code below to first create an array of the correct order of $_xm_anal['availability']['BLK'] keys:
$tmp = $_xm_anal['text_size'];
asort($tmp);
$tmp2 = array_keys($tmp);
This produces the array $tmp2 that equals to:
Array
(
[0] => SIZE24
[1] => SIZE4
[2] => SIZE8
[3] => SIZE10
[4] => SIZE14
[5] => SIZE16
[6] => SIZE19
)
So the only problem now is that I can't find the correct flag to use in the appropriate array_multisort to sort out the initial $_xm_anal array based on that $tmp2 array...
array_multisort($tmp2, SORT_NATURAL, $_xm_anal["availability"][$Prod["color_code"]]);
What is the correct flag to use to actually use the $tmp2 in the order its keys are met?
OK, I was able to sort it a bit indirectly/less elegant, but I couldn't think of any better solution... So I did this:
$tmp = $_xm_anal['text_size'];
asort($tmp);
foreach ($tmp as $kkk => $vvv) {
$tmp[$kkk]=$_xm_anal["availability"][$Prod["color_code"]][$kkk];
}
$_xm_anal["availability"][$Prod["color_code"]] = $tmp;
If anyone can think of a more elegant solution, please feel free to post it! TIA

merge/sum multi dimentional array php

I'm trying to merge/sums 2 arrays that can contain integers or more arrays (themselves containing integer).
When the values are integers, I need to sum them in the final array.
When the values are arrays, I need to loop through the values and sum them in the final array.
If a value or a sub-array exists only in 1 of the base array, it needs to be added in the sub-array of the final array. (This is what I can't do)..)
My arrays are like this:
ARRAY 1
[1466859600] => Array
(
[TOTAL] => 27217
[AAA] => Array
(
[FD_CDP] => 1746
[LO_SC_MIC] => 4654
[FD_ATS] => 893
[CDP] => 40
[SUPERVISION] => 9
[CONTROL] => 4
[ATS] => 4
[EVT_ACK] => 3
)
[BBB] => Array
(
[FD_CDP] => 1376
[LO_SC_MIC] => 4606
[FD_ATS] => 826
[FD_ATSS] => 451
[LO_SFRC] => 4
[FD_S2] => 259
[2_LOSC] => 2
)
[CCC] => Array
(
[FD_CDP] => 1333
[LO_SC_MIC] => 4725
[FD_ATS] => 856
[CONTROL] => 4
[ATS] => 2
[EVT_ACK] => 5
)
ARRAY 2
[1466859600] => Array
(
[TOTAL] => 95406
[AAA] => Array
(
[FD_ATSS] => 1719
[LO_SC_MIC] => 16830
[CONTROL] => 16
[NEW] => 7
[NOEL] => 206
)
[BBB] => Array
(
[SUPERVISION] => 23
[CDP] => 158
[CONTROL] => 40
[2_LOSC] => 14
[ATS] => 6
[EVT_ACK] => 4
)
[CCC] => Array
(
[EVT_ACK] => 167
[LO_SFRC] => 248
[SUPERVISION] => 23
)
I wrote a function like this :
function sumArrayValues($array1, $array2)
{
foreach ($array1 as $key => $value)
{
if (is_array($array1[$key]))
{
echo "it's an array\n I need to reloop\n";
sumArrayValues($array1[$key], $array2[$key]);
}
else
{
echo "FIRST VALUE TO SUM\n";
print_r($array1[$key]."\n");
echo "SECOND VALUE TO SUM\n";
print_r($array2[$key]."\n");
$array1[$key] = (int)$array1[$key] +(int)$array2[$key];
echo "--------RESULT of SUM array1&2----------\n";
}
}
return $array1;
}
But this function doesn't take into account 2 (and probably more) cases: if the sub-array are not in the same order, if a sub-array or a value only exist in second array.
A example of function would be a good help, but on a more fundamental level, I even can't figure the algorithm to do that.
Any ideas ?
You can get all the keys for the foreach loop, live demo.
Note, you also can check if a key of any array is undefined, then save the defined value for the key.
function sumArrayValues($array1, $array2)
{
$keys = array_keys($array1 + $array2);
foreach ($keys as $key)
{
if (is_array($array1[$key]) || is_array($array2[$key]))
$array1[$key] = sumArrayValues($array1[$key], $array2[$key]);
else
#$array1[$key] = (int)$array1[$key] +(int)$array2[$key];
}
return $array1;
}

find max value of a key in array

I have an array
[DoctorEducation] => Array
(
[0] => Array
(
[id] => 24
[user_id] => 91
[degree_type_id] => 1
[college_hospital] => sms
[diploma_name] =>
[specialization_id] => 0
[start_date] => 02/2009
[end_date] => 03/2012
[year_passing] => 0000
[created] => 2015-10-09 13:14:23
[updated] => 2015-10-09 13:16:18
)
[1] => Array
(
[id] => 26
[user_id] => 91
[degree_type_id] => 5
[college_hospital] => sms
[diploma_name] =>
[specialization_id] => 48
[start_date] => 03/2012
[end_date] => 05/2014
[year_passing] => 0000
[created] => 2015-10-09 13:16:18
[updated] => 2015-10-09 13:16:18
)
)
Now I want to find out which index i.e 0 or 1 or so on have the biggest value of degree_type_id. for example in current array index 1 has the biggest value of degree_type_id i.e 5.
I am getting this from DB. Here is the query
$fields = array(
'User.id',
'User.first_name',
'User.last_name',
'User.gender',
'User.dob',
'User.image',
'Specialization.name',
'User.age'
);
$getSpecialist = $this->User->find('all', array('fields' => $fields), array('conditions' => array('User.role_id' => 3, 'User.status' => 1)));
Try something like this:
$max_index = null;
$max_value = 0;
foreach($DoctorEducation as $key => $array){
if($array['degree_type_id'] > $max_value){
$max_value = $array['degree_type_id'];
$max_index = $key;
}
}
print_r($DoctorEducation[$max_index]);
This gives you the index and the value of the key with the highest degree_type_id
There may be a faster way to do this, but here's my solution:
$to_compare = array();
foreach($DoctorEducation as $idx => $arr){
$to_compare[$idx] = $arr['degree_type_id'];
}
$max = $to_compare[array_search(max($to_compare), $to_compare)];
Pretty simple with several functions:
$key = array_search(max(array_column($array['DoctorEducation'], 'degree_type_id')),
$array['DoctorEducation']);
Extract the appropriate column to an array
Find the maximum value in that column array
Search that column array for the maximum value, which returns the key
If there is more than one occurrence of the maximum value then you will get the first one.

updating php multidimensional array and resaving to db

I have the following structure for my array which is dynamically generated:
Array
(
[0] => Array
(
[friend_request_sender_id] => 1
[friend_request_date] => 07-08-2014
[friend_request_time] => 11:12:19
[friend_request_recipient_id] => 5
[friend_request_sent] => 1
[friend_request_accepted] => 0
)
[1] => Array
(
[friend_request_sender_id] => 1
[friend_request_date] => 07-08-2014
[friend_request_time] => 11:12:47
[friend_request_recipient_id] => 2
[friend_request_sent] => 1
[friend_request_accepted] => 0
)
)
I would like to update the first array value [friend_request_accepted] => 0 to this: [friend_request_accepted] => 1 which i have achieved by doing the following:
$test = get_user_meta($current_user->ID,'get_user_friends', true );
foreach($test as &$values){
if($values['friend_request_recipient_id'] === '5'){
$values['friend_request_accepted'] = '1';
break; // Stop the loop after we've found the item
}
}
However, i would like to actually save this new value into the database, overwriting the existing value. The array should then look like this:
Array
(
[0] => Array
(
[friend_request_sender_id] => 1
[friend_request_date] => 07-08-2014
[friend_request_time] => 11:12:19
[friend_request_recipient_id] => 5
[friend_request_sent] => 1
[friend_request_accepted] => 1
)
[1] => Array
(
[friend_request_sender_id] => 1
[friend_request_date] => 07-08-2014
[friend_request_time] => 11:12:47
[friend_request_recipient_id] => 2
[friend_request_sent] => 1
[friend_request_accepted] => 0
)
)
How should I do this?
this line doesn't work:
if($values['friend_request_recipient_id'] === '5'){
$values['friend_request_accepted'] = '1';
break; // Stop the loop after we've found the item
}
because
[friend_request_recipient_id] => 5
is an integer I think you just need to sure in condition because '===' is also checked the types of variable.

php turning array into multi-dimensional array

i have a (dynamic) array, which in this example contains 4 sets of data (5 fields per set), but it could be only one set or up to 25 sets.
Array ( [lightwattage1] => 50 [lightvoltage1] => 12 [lightquantity1] => 2 [lightusage1] => 4 [lightcomment1] => [lightwattage2] => 60 [lightvoltage2] => 24 [lightquantity2] => 4 [lightusage2] => 5 [lightcomment2] => [lightwattage3] => 30 [lightvoltage3] => 240 [lightquantity3] => 4 [lightusage3] => 2 [lightcomment3] => [lightwattage4] => 25 [lightvoltage4] => 12 [lightquantity4] => 2 [lightusage4] => 6 [lightcomment4] => )
which i'd like to turn into something like
Array ( Array ( [lightwattage1] => 50 [lightvoltage1] => 12 [lightquantity1] => 2 [lightusage1] => 4 [lightcomment1] => ),
Array ( [lightwattage2] => 60 [lightvoltage2] => 24 [lightquantity2] => 4 [lightusage2] => 5 [lightcomment2] => ),
Array ( [lightwattage3] => 30 [lightvoltage3] => 240 [lightquantity3] => 4 [lightusage3] => 2 [lightcomment3] => ),
Array ( [lightwattage4] => 25 [lightvoltage4] => 12 [lightquantity4] => 2 [lightusage4] => 6 [lightcomment4] => )
)
the original array is created this way:
$light = Array();
foreach( $_POST as $key => $val )
{
//field names that start with light to go in this array
if (strpos($key, 'light') === 0) {
$light[$key] = $val;
}
}
the field name digit is already added with JS before form submission, and not by php script.
any hint much appreciated.
This is not an exacty answer to you question, but...
You can use arrays in POST variables like so:
<input name="light[1][wattage]" />
<input name="light[1][voltage]" />
<input name="light[2][wattage]" />
<input name="light[2][voltage]" />
will give you:
$_POST['light'] == array(
1 => array(
'wattage' => '...',
'voltage' => '...',
),
2 => array(
'wattage' => '...',
'voltage' => '...',
),
)
Try this:
$prefixes = array();
$postfixes = array();
foreach($original_array as $key=>$value)
{
preg_match('/^([^\d]*)(\d+)$/',$key,$matches);
if(count($matches)>1)
{
if(!in_array($matches[1], $prefixes)) $prefixes[] = $matches[1];
if(!in_array($matches[2], $postfixes)) $postfixes[] = $matches[2];
}
}
$new_array = array();
foreach($postfixes as $postfix)
{
$new_element = array();
foreach($prefixes as $prefix)
{
if(isset($original_array[$prefix.$postfix])) $new_element[$prefix.$postfix] = $original_array[$prefix.$postfix];
}
$new_array[] = $new_element;
}
given an $original_array equal to described, will produce $new_array:
Array
(
[0] => Array
(
[lightwattage1] => 50
[lightvoltage1] => 12
[lightquantity1] => 2
[lightusage1] => 4
[lightcomment1] =>
)
[1] => Array
(
[lightwattage2] => 60
[lightvoltage2] => 24
[lightquantity2] => 4
[lightusage2] => 5
[lightcomment2] =>
)
[2] => Array
(
[lightwattage3] => 30
[lightvoltage3] => 240
[lightquantity3] => 4
[lightusage3] => 2
[lightcomment3] =>
)
[3] => Array
(
[lightwattage4] => 25
[lightvoltage4] => 12
[lightquantity4] => 2
[lightusage4] => 6
[lightcomment4] =>
)
)
I was uncertain about how much you knew about the elements or their order, so this code basically takes any collection of elements that end in numbers and rearranges them in groups that have the same ending number.
Try this:
$outarr = array()
$subarr = array()
$i=0;
foreach($_POST as $key => $val)
{
//only include keys starting with light:
if (strpos("light", $key)==0)
{
//create a new subarray each time we find a key that starts with "lightwattage":
if ($i>0 && strpos("lightwattage", $key)==0)
{
$outarr[] = $subarr;
}
$subarr[$key] = $val;
$i++;
}
}
$outarr[] = $subarr;
//$outarr contains what you want here

Categories