1

This works but is there a better way to do this with php, I want to get the highest value in all of the arrays, If someone could explain in easy terms ,-,

<?php
$marks1 = array(360,310,310,330,313,375,456,111,256);
$marks2 = array(350,340,356,330,321);
$marks3 = array(630,340,570,635,434,255,298);
$i=0;



foreach ($marks1 as $value)  
{   
        if($i > $value || $i == 0)
        $i=$value;
        {
          foreach ($marks2 as $value)
            {
                if($i > $value || $i == 0) 
                    $i=$value;
                foreach ($marks3 as $value)
                {
                    if($i < $value || $i == 0)
                        $i=$value; 
                }                                   

            }
        }   
}
echo "$i<br>";

?>
4
  • How about trying max function or you are asking specifically for this logic? Commented Sep 26, 2021 at 6:19
  • 3
    Hint: 3 nested loops is probably the MOST INEFFICIENT method possible: en.wikipedia.org/wiki/Big_O_notation Commented Sep 26, 2021 at 6:19
  • yeah not a good idea. :D Commented Sep 26, 2021 at 6:21
  • stackoverflow.com/questions/6676768/… Commented Sep 26, 2021 at 6:22

4 Answers 4

2

You can merge the arrays together and then use the max function.

$marks1 = array(360,310,310,330,313,375,456,111,256);
$marks2 = array(350,340,356,330,321);
$marks3 = array(630,340,570,635,434,255,298);

$merged = array_merge($marks1, $marks2, $marks3);

$max = max($merged);
Sign up to request clarification or add additional context in comments.

Comments

2

If you want to try out with your own max function logic.

You can also try with this way by merging arrays using array_merge function, this will combine all your arrays and will give you the highest value:

<?php
    function get_max_value($my_array){
      $n = count($my_array);
      $max_val = $my_array[0];
      for ($i = 1; $i < $n; $i++)
         if ($max_val < $my_array[$i])
            $max_val = $my_array[$i];
      return $max_val;
    }
   
    $marks1 = array(360,310,310,330,313,375,456,111,256);
    $marks2 = array(350,340,356,330,321);
    $marks3 = array(630,340,570,635,434,255,298);
   
   $my_array = array_merge($marks1, $marks2, $marks3);
   print_r("The highest value of the array is ");
   echo(get_max_value($my_array));
?>

Let me know if it helps.

3 Comments

I have to do it without using functions, I am currently an intern and they gave me this task, But I understand I can push values and do it like that Thanks
@OsamaZubair, if there are limitations on how you should solve a specific problem, please include this in your question as it helps others find an appropriate solution.
@OsamaZubair In that case, you can still use 3 foreach loops, but do them one after the other rather than nesting them.
2

If your php version more then 7.4 you can use Spread Operator. If not, the best way using array_merge($marks1, $marks2, $marks3)

<?php
$marks1 = array(360,310,310,330,313,375,456,111,256);
$marks2 = array(350,340,356,330,321);
$marks3 = array(630,340,570,635,434,255,298);

$max = max([...$marks1,...$marks2,...$marks3]);

echo $max; // Result: 635

Comments

1

Alternatively, this one can also work for you:

<?php
    
  $marks1 = array(360,310,310,330,313,375,456,111,256);
  $marks2 = array(350,340,356,330,321);
  $marks3 = array(630,340,570,635,434,255,298);
   
  $arr = array_merge($marks1, $marks2, $marks3);
  $b = 0;
  for($i=0;$i<count($arr);$i++)
   {
      if ($arr[$i] > $b)
    {
          $b = $arr[$i];
      }
  }
  echo $b;
?>

// Output:
// 635

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.