/    Sign up×
Community /Pin to ProfileBookmark

PHP script and max() Value

Hi everyone,
Sorry for bad English and thanks for your help in advance ? I have to explain one kind of tricky problem I’ve encountered while coding. Here’s the point:

I need a script that essentially do this: extract the 5 max values of 5 arrays, that are “mixed”, i.e. they contain “recurrent” values. Here is an example:

array1(a, b)
array2(a, c, d, e, g)
array3(b, d, g, h)
array4(e, t, z)
array5(b, c, d, k)

[B]The 2 essential request are the following:
1) the sum of those 5 arrays (array1+array2+array3…) MUST be the highest possible…
2) …without repeat ANY value previously used[/B]
(e.g. if in array1 the top value was “b”, this cannot be re-taken as max value in the arrays 3 or 5).

Currently I coded like this…:

Code: Select all

[code=php] $group1 = array(a, b);
$group = array(a, b, c, d);

$max1a = max(group1);
$max2a = max(group2) unset($max1a);

$sum1 = $max1a + $max2a;

$max2b = max(group2);
$max1b = max(group1) unset($max2b);

$sum2 = $max1b + $max2b;

if($sum1 > $sum2) {echo $sum1}
else {echo $sum2} [/code]

… but it’s kinda impossible to use this code with 5 arrays, because I should compare 5! (120 ๐Ÿ˜ฎ ) combinations in order to find the max sum value.

I know the problem is quite difficult to explain and to solve, but I very need your help and I hope you can save me!!! ?
Cheers

to post a comment
PHP

18 Comments(s) โ†ด

Copy linkTweet thisAlerts:
@GravyAug 10.2014 โ€”ย I'm not quite understanding your question, but I'll take a stab in the dark that what you want is:

The highest 5 maximum unique values throughout the 5 arrays.

[code=php]
$g1 = ['a','b','d','f','h'];
$g2 = ['a','k','d','f','h'];
$g3 = ['l','z','k','d','f','h'];
$g4 = ['l','s','k','d','f','h'];
$g5 = ['l','z','k','d','l','h'];
// Turn our 5 arrays into 1 big array, then take out all duplicates
$full = array_unique(array_merge($g1,$g2,$g3,$g4,$g5));
// These following 2 lines will result in $list = ['z','s','l','k','h'];
rsort($full);
$list = array_slice($full, 0, 5);
// These following 2 lines will result in $list = ['h','l','s','k','z'];
sort($full);
$list = array_slice($full, -5);
// Print out our list
print_r($list);
[/code]


I'm really not sure how you're trying to add characters together?

If they were numbers and not letters...

[code=php]
echo array_sum($list);
[/code]
Copy linkTweet thisAlerts:
@ginerjmAug 10.2014 โ€”ย The question is do you want the max value of each array in a specific order? If you have two values of b and you take b from one array, should the other array have precedence over the one you just took?

Otherwise - sort each array in descending order. Then take the first element and see if it already exists in your result array. If so, take the next and so on. If not, move to the next array and repeat until done.
Copy linkTweet thisAlerts:
@gfr1991authorAug 10.2014 โ€”ย The question is do you want the max value of each array in a specific order? If you have two values of b and you take b from one array, should the other array have precedence over the one you just took?

Otherwise - sort each array in descending order. Then take the first element and see if it already exists in your result array. If so, take the next and so on. If not, move to the next array and repeat until done.[/QUOTE]


Thank you for your reply! Here's the point: No array should have "precedence" over anyone else; max values should be choosen in order to get the highest possible sum when adding all of the arrays.

Here's an example.

a=30

B=28

C=20

D=15.

Array1(a,b)

Array2(a,c,d)

If I put arrays in descending order, I'll have array1=30, array2=20 (because I've excluded A from array2). Sum is 50, but this is not the highest sum possible. In fact, If I take A for array2 and B for Array1, I'd get 58 (which is actually the highest sum possible). Obviously I can't know before values for a, b, c etc.

Think I need to sort all the values in the arrays and then choose the combination which returns me the highest sum for all arrays.

Any idea? ?
Copy linkTweet thisAlerts:
@ginerjmAug 10.2014 โ€”ย Well, you seem to have all the situations figured out. Now - just start writing some code. This is what programming is all about - figuring out algorithms to solve problems.
Copy linkTweet thisAlerts:
@NogDogAug 10.2014 โ€”ย Something like this?
[code=php]
<?php
$a = array(10,20,30,40);
$b = array(60,50,40,30);
$c = array(55,50,45,40,35,30);
$d = array(60,40,20,0);

$merged = array_unique(array_merge($a,$b,$c,$d));
rsort($merged);
$result = array_sum(array_slice($merged, 0, 5));
echo "Sum of top 5 of".PHP_EOL.print_r($merged,1).PHP_EOL."is: $result".PHP_EOL;
[/code]
Copy linkTweet thisAlerts:
@ginerjmAug 10.2014 โ€”ย Hmmm...

If the 60 from array b is taken, that leaves a 40 from array d. Yet the OP wants the 60 from array d to be used since that would then include the 50 from array b.

No?
Copy linkTweet thisAlerts:
@NogDogAug 10.2014 โ€”ย I'm just taking the 5 largest unique values from all the arrays. However, I'll admit I'm not 100% sure that fulfills the OP's requirements. ?
Copy linkTweet thisAlerts:
@ginerjmAug 10.2014 โ€”ย See post #4
Copy linkTweet thisAlerts:
@NogDogAug 10.2014 โ€”ย So...if the objective is to get the highest total possible by selecting one and only one value from each array, without duplicating any values, you're probably looking at some sort of recursive function that will look for all permutations. Of course, that begs the question, what should the result be for this?
[code=php]
$array1 = (50,40,30);
$array2 = (50,40,30);
$array3 = (50,40,30);
$array4 = (50,40,30);
[/code]

?
Copy linkTweet thisAlerts:
@gfr1991authorAug 10.2014 โ€”ย Firstly, thank you for your reply! And sorry but I'm facing troubles even in explaining this problem ?

I'm not quite understanding your question, but I'll take a stab in the dark that what you want is:

[b]The highest 5 maximum unique values throughout the 5 arrays.[/b]
[/QUOTE]



Unfortunately no. If I choose the highest 5 maximum unique values in all arrays, I'll may risk to take two values belonging to one single arrays, and I can't. Going with your example... (I've to modified it a bit, cause I can't know before if the arrays are so "similar"):


[code=php]$g1 = ['a','b','c','d','h'];
$g2 = ['c','k','d','f','h'];
$g3 = ['l','z','k','d','f','h'];
$g4 = ['l','s','k','d','f','h'];
$g5 = ['l','z','k','d','l','h'];[/code]

[/QUOTE]


...if the highest 5 values are, for example, a, b, c, d and h, I will take [B]2[/B] value for g1 (a and b), 1 for g2 (c), 1 for g3 (d) and 1 for g4 (h), but nothing for g5, because I can't re-take a value I already take in another array.

Kind of a good code, however. If arrays are so similar, I bet it'll work ?
Copy linkTweet thisAlerts:
@gfr1991authorAug 10.2014 โ€”ย Well, you seem to have all the situations figured out. Now - just start writing some code. This is what programming is all about - figuring out algorithms to solve problems.[/QUOTE]

You're right, but [I]understand[/I] the problem is not quite [I]solve[/I] it. This is the reason I called for help ?

So...if the objective is to get the highest total possible by selecting one and only one value from each array, without duplicating any values, you're probably looking at some sort of recursive function that will look for all permutations. Of course, that begs the question, what should the result be for this?
[code=php]
$array1 = (50,40,30);
$array2 = (50,40,30);
$array3 = (50,40,30);
$array4 = (50,40,30);
[/code]

?[/QUOTE]


Thank you for your reply too! However, it depends. You took numbers, but sadly I'm messed up with strings (or letters in the examples) ? . It's quite difference. Look at this example:

a=50

b=50

c=50

d=40

e=40

f=30

g=30

so, consider...
[code=php]
//EXAMPLE 1
$array1 = (a,d,f);
$array2 = (b,d,f);
$array3 = (a,e,f);
$array4 = (c,d,g);

//EXAMPLE 2
$array1 = (a,d,f);
$array2 = (a,d,f);
$array3 = (a,d,f);
$array4 = (a,e,g);
[/code]


...which are quite the same of (50,40,30) you posted above. EXAMPLE1 returns 190 as result (take a, b, e and c respectively in array1, 2, 3 and 4); EXAMPLE2 return 160 instead (take a, d, f and e respectively in array1, 2, 3 and 4).
Copy linkTweet thisAlerts:
@NogDogAug 10.2014 โ€”ย But you avoided this case I asked about (only 3 unique values across 4 arrays):
[code=php]
//EXAMPLE 2
$array1 = (a,d,f);
$array2 = (a,d,f);
$array3 = (a,d,f);
$array4 = (a,d,f);
[/code]

It's not that I want to be a trouble-maker (well, maybe a bit), but without really understanding the requirements (e.g.: what do these values actually represent and why do you want to pick this particular combination of values?), there is no guarantee any solution we come up with actually provides the functionality your application really needs.
Copy linkTweet thisAlerts:
@gfr1991authorAug 10.2014 โ€”ย But you avoided this case I asked about (only 3 unique values across 4 arrays):
[code=php]
//EXAMPLE 2
$array1 = (a,d,f);
$array2 = (a,d,f);
$array3 = (a,d,f);
$array4 = (a,d,f);
[/code]

It's not that I want to be a trouble-maker (well, maybe a bit), but without really understanding the requirements (e.g.: what do these values actually represent and why do you want to pick this particular combination of values?), there is no guarantee any solution we come up with actually provides the functionality your application really needs.[/QUOTE]


I see. Given a>d>f, the result I expect from your example is a+d+f. In fact I pick each variable only one time; for the fourth array (which - I want to emphasize - it's not mandatory to be array4) I can't took any value, so 0.
Copy linkTweet thisAlerts:
@GravyAug 10.2014 โ€”ย [code=php]
$g1 = [50,40,30];
$g2 = [20,10,2];
$g3 = [4,3,5];
$g4 = [4,8,6];
[/code]


If the answer to the above is 150, the code you want is below. (50+40+30+20+10)

[code=php]
$g1 = [50,40,30];
$g2 = [20,10,2];
$g3 = [4,3,5];
$g4 = [4,8,6];
// Turn our 5 arrays into 1 big array, then take out all duplicates
$full = array_unique(array_merge($g1,$g2,$g3,$g4));
// These folling 2 lines will result in $list = ['z','s','l','k','h'];
rsort($full);
$list = array_slice($full, 0, 5);
// Print out our list
$result = array_sum($list);
echo $result;
[/code]


Is this the result you are after?
Copy linkTweet thisAlerts:
@NogDogAug 10.2014 โ€”ย Here's my latest (possibly overly complex) possibility:
[code=php]
<?php

function myArraySum()
{
$data = func_get_args();
foreach($data as $key => $val) {
rsort($data[$key]);
}
usort(
$data,
function($a, $b) {
for($i=0; $i<(min(count($a), count($b))); $i++) {
if($a[$i] != $b[$i]) {
return $b[$i] - $a[$i];
}
}
return 0;
}
);
$data = array_reverse($data);
$final = array();
foreach($data as $arr) {
foreach($arr as $value) {
if(!in_array($value, $final)) {
$final[] = $value;
break;
}
}
}
//print_r($data);
//print_r($final);
return array_sum($final);
}

// TEST:

$a = 30;
$b = 25;
$c = 20;
$d = 15;
$e = 10;

$array1 = array($a, $b, $d);
$array2 = array($a, $c, $d);
$array3 = array($b, $c, $d);
$array4 = array($b, $d, $e);

echo myArraySum($array1, $array2, $array3, $array4);
[/code]
Copy linkTweet thisAlerts:
@gfr1991authorAug 10.2014 โ€”ย Here's my latest (possibly overly complex) possibility:
[code=php]
<?php

function myArraySum()
{
$data = func_get_args();
foreach($data as $key => $val) {
rsort($data[$key]);
}
usort(
$data,
function($a, $b) {
for($i=0; $i<(min(count($a), count($b))); $i++) {
if($a[$i] != $b[$i]) {
return $b[$i] - $a[$i];
}
}
return 0;
}
);
$data = array_reverse($data);
$final = array();
foreach($data as $arr) {
foreach($arr as $value) {
if(!in_array($value, $final)) {
$final[] = $value;
break;
}
}
}
//print_r($data);
//print_r($final);
return array_sum($final);
}

// TEST:

$a = 30;
$b = 25;
$c = 20;
$d = 15;
$e = 10;

$array1 = array($a, $b, $d);
$array2 = array($a, $c, $d);
$array3 = array($b, $c, $d);
$array4 = array($b, $d, $e);

echo myArraySum($array1, $array2, $array3, $array4);
[/code]
[/QUOTE]


Very good code, actually! Very thanks for your help, but there's still one little problem.

For example:

[code=php]
$a = 30;
$b = 30;
$c = 20;
$d = 15;
$e = 10;

$array1 = array($a, $c);
$array2 = array($b, $d, $e);

echo myArraySum($array1, $array2);[/code]


I expect the code to give me back the value of 60, but it returns me 50. In fact, $a and $b has the same numeric value, but they are two different elements in my arrays (think they as two university exams: you could get "30" (which in Italy is the same as A+) at both, but they are still two different exams).

In other words, it is possible to refers to string instead of numbers in your snippet?

Thx in advance again!
Copy linkTweet thisAlerts:
@NogDogAug 11.2014 โ€”ย Well, you can do numeric sorts, and you can do string sorts, but combining both is going to be tricky at best. Without explicit rules for what exactly is going to be in your arrays and how they are to be sorted, there's not a lot more I can do. You may need yet another array that relates what the value of each "thing" in the data arrays actually is, then use that in the usort callback function, as well as when building the final array of selected values.
Copy linkTweet thisAlerts:
@GravyAug 11.2014 โ€”ย Well, If you like NogDogs, this one should do what you want. Note with this change you can use 'A' or 'a' to equal 30, or just a number.

[code=php]
<?php
function myArraySum()
{
$data = func_get_args();
$markingIndex = [
'a' => 30,
'A' => 30,
'b' => 30,
'B' => 30,
'c' => 20,
'C' => 20,
'd' => 15,
'D' => 15,
'e' => 10,
'E' => 10,
];
foreach ($data as $key => &$markArray) {
foreach ($markArray as $key => &$marks) {
if (isset($markingIndex[$marks])) {
$marks = $markingIndex[$marks];
}
}
}
foreach($data as $key => $val) {
rsort($data[$key]);
}
usort(
$data,
function($a, $b) {
for($i=0; $i<(min(count($a), count($b))); $i++) {
if($a[$i] != $b[$i]) {
return $b[$i] - $a[$i];
}
}
return 0;
}
);
$data = array_reverse($data);
$final = array();
foreach($data as $arr) {
foreach($arr as $value) {
if(!in_array($value, $final)) {
$final[] = $value;
break;
}
}
}
//print_r($data);
//print_r($final);
return array_sum($final);
}

// TEST:

$a = 'a';
$b = 25;
$c = 'c';
$d = 15;
$e = 10;

$array1 = array($a, $b, $d);
$array2 = array($a, $c, $d);
$array3 = array($b, $c, $d);
$array4 = array($b, $d, $e);

echo myArraySum($array1, $array2, $array3, $array4);
[/code]
ร—

Success!

Help @gfr1991 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 โ€” BETA 5.20,
whats_new: community page,
up_next: more Davinciโ€ข003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinciโ€ข003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...