find max value of a key in array - php

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.

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

Sum parts of an array in php

this is quite beyond me. Appreciate some help.
I have an array in php like so:
[0] => Array
(
[cust_id] => 1006
[no_of_subs] => 2
[dlv_id] => 1000
)
[1] => Array
(
[cust_id] => 1011
[no_of_subs] => 3
[dlv_id] => 1000
)
[2] => Array
(
[cust_id] => 1012
[no_of_subs] => 5
[dlv_id] => 1001
)
[3] => Array
(
[cust_id] => 1013
[no_of_subs] => 6
[dlv_id] => 1001
)
I don't need the cust_id field. I just need to group the dlv_id and the sum of no_of_subs for each matching dlv_id. The result should look like this:
[0] => Array
(
[dlv_id] => 1000
[no_of_subs] => 5
)
[1] => Array
(
[cust_id] => 1011
[no_of_subs] => 11
)
Thank you for any help.
I don't understand the downvotes for this question. Am i doing it all wrong? Downvoting without a reason is not helping.
The simplest, most efficient way to group and sum is to perform a single loop and assign temporary associative keys.
When a row is identified as a new dlv_id row, save the two desired elements, otherwise add the no_of_subs value to the pre-existing value.
Optionally, remove the temporary keys with array_values().
Code (Demo)
$array = [
["cust_id" => 1006, "no_of_subs" => 2, "dlv_id" => 1000],
["cust_id" => 1011, "no_of_subs" => 3, "dlv_id" => 1000],
["cust_id" => 1012, "no_of_subs" => 5, "dlv_id" => 1001],
["cust_id" => 1013, "no_of_subs" => 6, "dlv_id" => 1001]
];
foreach ($array as $row) {
if (!isset($result[$row["dlv_id"]])) {
$result[$row["dlv_id"]] = ["dlv_id" => $row["dlv_id"], "no_of_subs" => $row["no_of_subs"]];
} else {
$result[$row["dlv_id"]]["no_of_subs"] += $row["no_of_subs"];
}
}
var_export(array_values($result));
Output:
array (
0 =>
array (
'dlv_id' => 1000,
'no_of_subs' => 5,
),
1 =>
array (
'dlv_id' => 1001,
'no_of_subs' => 11,
),
)
Using array_column function, we can extract out dlv_id and no_of_subs separately in two different arrays, using cust_id as the key.
Now, simply loop over the array of dlv_id, and if matching key found, add the no_of_subs to it, else set the value (for the first time).
We use isset function to check if the key exists already or not.
Try the following:
// your input array is $input_array
// get all dlv_id maintaining the cust_id as index
$dlv_id = array_column($input_array, 'dlv_id', 'cust_id');
// get all no_of_subs maintaining the cust_id as index
$no_of_subs = array_column($input_array, 'no_of_subs', 'cust_id');
$output = array();
foreach ($dlv_id as $key => $value) {
if (isset($output[$value]['dlv_id'])) {
$output[$value]['dlv_id'] += $no_of_subs[$key];
} else {
$output[$value]['dlv_id'] += $no_of_subs[$key];
}
}

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;
}

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: How can i combine 2 different array into one

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];
}

Categories