www.webdeveloper.com
Page 2 of 2 FirstFirst 12
Results 16 to 19 of 19

Thread: PHP script and max() Value

  1. #16
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,388
    Here's my latest (possibly overly complex) possibility:
    PHP Code:
    <?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);
    "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

  2. #17
    Join Date
    Aug 2014
    Posts
    6
    Quote Originally Posted by NogDog View Post
    Here's my latest (possibly overly complex) possibility:
    PHP Code:
    <?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);
    Very good code, actually! Very thanks for your help, but there's still one little problem.
    For example:

    PHP Code:
    $a 30;
    $b 30;
    $c 20;
    $d 15;
    $e 10;

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

    echo 
    myArraySum($array1$array2); 
    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!

  3. #18
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,388
    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.
    "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

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

    PHP Code:
    <?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);

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