www.webdeveloper.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 19

Thread: PHP script and max() Value

  1. #1
    Join Date
    Aug 2014
    Posts
    6

    Question 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)

    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
    (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
    PHP Code:
     $group1 = array(ab);
        
    $group = array(abcd);

        
    $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

    ... 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

  2. #2
    Join Date
    May 2014
    Posts
    75
    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.

    PHP Code:
    $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($full05);
    // 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); 
    I'm really not sure how you're trying to add characters together?
    If they were numbers and not letters...

    PHP Code:
    echo array_sum($list); 

  3. #3
    Join Date
    Jul 2013
    Location
    Voorheesville NY USA
    Posts
    751
    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.
    JG
    PS - If you're posting here you should be using:

    error_reporting(E_ALL | E_NOTICE);
    ini_set('display_errors', '1');


    at the top of ALL php code while you develop it!

  4. #4
    Join Date
    Aug 2014
    Posts
    6

    Question

    Quote Originally Posted by ginerjm View Post
    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.
    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?

  5. #5
    Join Date
    Jul 2013
    Location
    Voorheesville NY USA
    Posts
    751
    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.
    JG
    PS - If you're posting here you should be using:

    error_reporting(E_ALL | E_NOTICE);
    ini_set('display_errors', '1');


    at the top of ALL php code while you develop it!

  6. #6
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,357
    Something like this?
    PHP Code:
    <?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($merged05));
    echo 
    "Sum of top 5 of".PHP_EOL.print_r($merged,1).PHP_EOL."is: $result".PHP_EOL;
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  7. #7
    Join Date
    Jul 2013
    Location
    Voorheesville NY USA
    Posts
    751
    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?
    JG
    PS - If you're posting here you should be using:

    error_reporting(E_ALL | E_NOTICE);
    ini_set('display_errors', '1');


    at the top of ALL php code while you develop it!

  8. #8
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,357
    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.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  9. #9
    Join Date
    Jul 2013
    Location
    Voorheesville NY USA
    Posts
    751
    See post #4
    JG
    PS - If you're posting here you should be using:

    error_reporting(E_ALL | E_NOTICE);
    ini_set('display_errors', '1');


    at the top of ALL php code while you develop it!

  10. #10
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,357
    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?
    PHP Code:
    $array1 = (50,40,30);
    $array2 = (50,40,30);
    $array3 = (50,40,30);
    $array4 = (50,40,30); 
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  11. #11
    Join Date
    Aug 2014
    Posts
    6

    Smile

    Firstly, thank you for your reply! And sorry but I'm facing troubles even in explaining this problem

    Quote Originally Posted by Gravy View Post
    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.

    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"):

    PHP Code:
    $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']; 
    ...if the highest 5 values are, for example, a, b, c, d and h, I will take 2 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

  12. #12
    Join Date
    Aug 2014
    Posts
    6
    Quote Originally Posted by ginerjm View Post
    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.
    You're right, but understand the problem is not quite solve it. This is the reason I called for help

    Quote Originally Posted by NogDog View Post
    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?
    PHP Code:
    $array1 = (50,40,30);
    $array2 = (50,40,30);
    $array3 = (50,40,30);
    $array4 = (50,40,30); 
    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...
    PHP Code:
    //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); 
    ...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).

  13. #13
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,357
    But you avoided this case I asked about (only 3 unique values across 4 arrays):
    PHP Code:
    //EXAMPLE 2
    $array1 = (a,d,f);
    $array2 = (a,d,f);
    $array3 = (a,d,f);
    $array4 = (a,d,f); 
    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.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  14. #14
    Join Date
    Aug 2014
    Posts
    6
    Quote Originally Posted by NogDog View Post
    But you avoided this case I asked about (only 3 unique values across 4 arrays):
    PHP Code:
    //EXAMPLE 2
    $array1 = (a,d,f);
    $array2 = (a,d,f);
    $array3 = (a,d,f);
    $array4 = (a,d,f); 
    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.
    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.

  15. #15
    Join Date
    May 2014
    Posts
    75
    PHP Code:
    $g1 = [50,40,30];
    $g2 = [20,10,2];
    $g3 = [4,3,5];
    $g4 = [4,8,6]; 
    If the answer to the above is 150, the code you want is below. (50+40+30+20+10)

    PHP Code:
    $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($full05);
    // Print out our list
    $result array_sum($list);
    echo 
    $result
    Is this the result you are after?

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles