-1

hellom im new on php, and i have some trouble with my code

this message error

Message: max(): When only one parameter is given, it must be an array

for sum(value) i don't have troubles. as you can see on this images. Image 1

But, when I tried to get the max value, but instead got an error like this. image2 error message

this my error script (for find max value)

for ($i=0;$i<count($kriteria);$i++)
   {
      $pembagi[$i] = 0;
         for ($j=0;$j<count($alternatif);$j++)
            {
               $pembagi[$i] = $pembagi[$i] + max($alternatifkriteria[$j][$i]);
            }
    }

and this my good script for find sum(value)

 for ($i=0;$i<count($kriteria);$i++)
    {
       $pembagi[$i] = 0;
          for ($j=0;$j<count($alternatif);$j++)
             {
                $pembagi[$i] = $pembagi[$i] + ($alternatifkriteria[$j][$i]);
             }
     }

can someone help me? i need to get max(value) like this image results

this my array

$kriteria = [C1,C2,C3,C4,C5,C6];
$alternatif = [ALT1,ALT2,ALT,ALT4,ALT5,ALT6,ALT7];
arrayValue = (
[ALT1][C1] = 5
[ALT1][C2] = 1
[ALT1][C3] = 5
[ALT1][C4] = 2
[ALT1][C5] = 1
[ALT1][C6] = 5
[ALT2][C1] = 2
[ALT2][C2] = 4
[ALT2][C3] = 2
[ALT2][C4] = 5
[ALT2][C5] = 4
[ALT2][C6] = 4
[ALT3][C1] = 1
[ALT3][C2] = 4
[ALT3][C3] = 2
[ALT3][C4] = 5
[ALT3][C5] = 2
[ALT3][C6] = 4
[ALT4][C1] = 2
[ALT4][C2] = 4
[ALT4][C3] = 4
[ALT4][C4] = 5
[ALT4][C5] = 4
[ALT4][C6] = 2
[ALT5][C1] = 5
[ALT5][C2] = 4
[ALT5][C3] = 4
[ALT5][C4] = 1
[ALT5][C5] = 2
[ALT5][C6] = 4
[ALT6][C1] = 4
[ALT6][C2] = 4
[ALT6][C3] = 2
[ALT6][C4] = 5
[ALT6][C5] = 2
[ALT6][C6] = 4
[ALT7][C1] = 4
[ALT7][C2] = 2
[ALT7][C3] = 2
[ALT7][C4] = 5
[ALT7][C5] = 2
[ALT7][C6] = 1
);
  • It looks like `$alternatifkriteria[$j][$i]` isn't an array, but rather a single value in the array. Are you trying to find the max of all the values in the entire array? Or the max of the values in each row or column? The `max` function expects an array as its first parameter when only one parameter is passed (it doesn't make much sense to find the max of just one singular value). – Nathan Jan 05 '19 at 01:58
  • what do you expect `max()` to return if you pass only one particular value as an argument? php interprets when one argument passed, that it should be an array to find the max element. Do you have another opinion? – Alex Jan 05 '19 at 02:02
  • @Nathan i need to find max value for each column.I just added the results I wanted. see the latest updates – Adventure Marvelous Jan 05 '19 at 02:02
  • @Alex the most important point is how to get the max value, and I am confused to do it. – Adventure Marvelous Jan 05 '19 at 02:04
  • 1
    provide your array sample and expected result value or output – Alex Jan 05 '19 at 02:08
  • Please add the array you're using for testing to your question. – Nathan Jan 05 '19 at 02:19
  • @Alex for result output i have add it before, and now i add my array – Adventure Marvelous Jan 05 '19 at 02:27
  • @Nathan I just added it. – Adventure Marvelous Jan 05 '19 at 02:28

3 Answers3

1

You just need to include the existing value in the max computation:

for ($i=0;$i<count($kriteria);$i++)
   {
      $pembagi[$i] = 0;
         for ($j=0;$j<count($alternatif);$j++)
            {
               $pembagi[$i] = max($pembagi[$i], $alternatifkriteria[$j][$i]);
            }
    }

Update

Another way of doing this is to transpose the array (using array_map as described here) and then you can use array_sum and max to get the sum and maximum value for each $kriteria value:

// get the values with numeric keys...
$akv = array_values($alternatifkriteria);
// so we can unpack them to transpose the array...
$akt = array_map(null, ...$akv);
// now we can just sum and take the max of each column
$sums = array_map(function ($v) { return array_sum($v); }, $akt);
$maxs = array_map(function ($v) { return max($v); }, $akt);
print_r($sums);
print_r($maxs);

Output:

Array ( [0] => 23 [1] => 23 [2] => 21 [3] => 28 [4] => 17 [5] => 24 )
Array ( [0] => 5  [1] => 4  [2] => 5  [3] => 5  [4] => 4  [5] => 5 )

Demo on 3v4l.org

Nick
  • 138,499
  • 22
  • 57
  • 95
0

Your goal is not clear but probably this is something that can help you to move one step forward:

for ($i=0;$i<count($kriteria);$i++) {
      $pembagi[$i] = max($alternatifkriteria[$i]);
} 

UPDATE Here is another approach

 $j = 0;
 foreach ($alternatif as $column) {
     $pembagi[$j] = max(array_column($alternatifkriteria,$column));
     $j++;
 }
Alex
  • 16,739
  • 1
  • 28
  • 51
0

because of your main goal is for show max value for each row so, this code will help you, this code dont use max but the goal same as what you need.

$pembagi = array();
       for ($i=0;$i<count($kriteria);$i++)
          {
             $pembagi [$i] = 0;
             for ($j=0;$j<count($alternatif);$j++)
                {
                   if ($j == 0) 
                      { 
                         $pembagi [$i] = $alternatifkriteria[$j][$i];
                      }
                   else 
                      {
                         if ($pembagi [$i] < $alternatifkriteria[$j][$i])
                            {
                               $pembagi [$i] = $alternatifkriteria[$j][$i];
                            }
                      }
                  }
             }
AdityaDees
  • 1,022
  • 18
  • 39