首页 > 解决方案 > 数组值未传递给函数

问题描述

代码解释了查找最大值和最小值,但是,我在函数上传递了一个数组但是输出似乎没有出现在浏览器上,它是空白的。我假设逻辑是 100% 好的。另外,我希望有人教我如何在函数中传递数组的语法。因为我无法在 PHP 中传递数组值。一个解决方案会很棒

<?php
$test=array(9,7,3,13,1); // sample array
function maximum($array) // function to find maximum value in an array
{ 


 $slot=$array[0]; // fixed the first value of the array
 $length=count($array); // length of the array
 for($i=1; $i<$length; $i++)
 {
       $slot = ($slot<$array[$i]) ? $array[$i] : " "; // comparing the fixed value with other values
 }
 
 echo $slot."<br>"; // final max value
 
}

function minimum($array)  // function to find minimum value in an array
{
  
 
 $slot=$array[0]; // fixed the first value of the array
 $length=count($array);  // length of the array
 for($i=1; $i<$length; $i++)
 {
       
     $slot= ($slot>$array[$i]) ? $array[$i] : " "; // comparing the fixed value with other values
 }
 
 echo $slot; // final max value
 
}
echo maximum($test); //passing the $test array to find the max
echo minimum($test); //passing the $test array to find the min

?>  

标签: php

解决方案


要调用函数,只需使用以下格式

     maximum($test); 
     minimum($test); 

无需在函数前加上 echo

请把逻辑改成

$slot= ($slot>$array[$i]) ? $array[$i] : $slot;


推荐阅读