-1

i have data in array

[list] => Array
    (
        [0] => Array
            (
                [id] => 216
                [name] => item A
                [nilai] => 0.456
            )

        [1] => Array
            (
                [id] => 217
                [name] => item B
                [nilai] => 0.999
            )
    )

here I want to make a condition if the value is the largest then the text is green how to make the condition in foreach ?

this my code

<?php foreach($res['method']['list'] as $key=>$row) { ?>
     <div class="form-check">
        <input class="form-check-input" type="radio" name="flexRadioDefault" id="flexRadioDefault1">
        <label class="form-check-label" for="flexRadioDefault1"><?php echo $row['nilai'] ?></label>
    </div>
<?php } ?>
hellows
  • 15
  • 5
  • Does this answer your question? [PHP compare array](https://stackoverflow.com/questions/901815/php-compare-array) – Ainz Jun 28 '22 at 07:31
  • there are two approaches, sort it and print the first element of the array with the green color, OR loop over it and get the max value in in the other loop check if the value is equal to the max value then make it green – hassan Jun 28 '22 at 07:49
  • give me an example – hellows Jun 28 '22 at 07:50
  • There are good examples in the given link. I would use the solution in the given link to find the highest value and then use that in you're for loop. – Brian Jun 28 '22 at 07:55
  • [Find highest value in multidimensional array](https://stackoverflow.com/questions/17339421/find-highest-value-in-multidimensional-array) – mickmackusa Oct 08 '22 at 12:13
  • Your loop is creating duplicate `id` attributes within the `foreach()` which will render your html document invalid and generate an unpredictable/unwanted user experience. – mickmackusa Oct 08 '22 at 12:20

2 Answers2

5
  <?php
  $val_array = array_column($res['method']['list'], 'nilai');
  $hightestValueIndex = array_keys($val_array, max($val_array));
  foreach($res['method']['list'] as $key=>$row) { ?>
      <div class="form-check">
          <input class="form-check-input" type="radio" name="flexRadioDefault" id="flexRadioDefault1">
  <?php if ($key == $hightestValueIndex[0]){ ?>
          <label style="color:green;" class="form-check-label" for="flexRadioDefault1"><?php echo $row['nilai'] ?></label>
  <?php} else { ?>
  <label class="form-check-label" for="flexRadioDefault1"><?php echo $row['nilai'] ?></label>
      </div>
  <?php } } ?>

In the above code we at first extract the 'nilai' in separate and find max value and store it's index using that index in foreach loop we can achieve the desired result

  • It makes better sense to check `in_array($key, $hightestValueIndex)` in case multiple rows contain the max value. Only the `style="color:green;"` should be conditionally written; not the entire ` – mickmackusa Oct 08 '22 at 12:13
0

This will first calculate the maximum 'nilai' value in your list, and then in array_map a copy of your list will be created with an additional key 'max' for each item in your list. This value will be set to 'true' or 'false' depending on nihai being equal to $maxNilai or not. You then can use that 'max' boolean value to color in green or not.

$list = [
    [ 'id' => 216, 'name' => 'item A', 'nilai' => 0.456 ],
    [ 'id' => 217, 'name' => 'item B', 'nilai' => 0.999 ]
];

$maxNilai = max(array_column($list, 'nilai'));

$result = array_map(
    fn($item) => array_merge($item, [ 'max' => $item['nilai'] === $maxNilai ]),
    $list
);

print_r($result);

Output:

Array
(
    [0] => Array
        (
            [id] => 216
            [name] => item A
            [nilai] => 0.456
            [max] => 
        )

    [1] => Array
        (
            [id] => 217
            [name] => item B
            [nilai] => 0.999
            [max] => 1
        )

)
lukas.j
  • 6,453
  • 2
  • 5
  • 24
  • helo @lucas.j I use this function but error , the message : syntax error, unexpected '=>' (T_DOUBLE_ARROW), expecting ',' or ')' – hellows Jun 30 '22 at 09:43
  • Which PHP version are you using? – lukas.j Jun 30 '22 at 09:47
  • I'm using PHP version 7.1 – hellows Jun 30 '22 at 09:54
  • Then you have to replace the line starting with _fn($item) => ..._ with this code: _function ($item) use ($maxNilai) { return array_merge($item, [ 'max' => $item['nilai'] === $maxNilai ]); },:_ – lukas.j Jun 30 '22 at 09:54
  • It is the same code, but written as a function instead of the arrow function version. Arrow functions are available since PHP 7.4 (see https://www.php.net/manual/en/functions.arrow.php). – lukas.j Jun 30 '22 at 09:56
  • $result = array_map( function ($item) use ($maxNilai ) { return array_merge($item, [ 'max' => $item[' nilai'] === $maxNilai ]); } ); I'm like trying this but it still fails – hellows Jun 30 '22 at 10:05
  • Yes, because you deleted the line _$list_. The code I gave you only replaces the line starting with _fn($item) => ... _ – lukas.j Jun 30 '22 at 10:11
  • _array_map_ has two arguments, the function, and the list. – lukas.j Jun 30 '22 at 10:12
  • can you write the full code? thanks – hellows Jul 01 '22 at 01:27