获取具有不同变量的数组的值

时间:2013-11-23 16:30:57

标签: php arrays variables multidimensional-array

我必须创建三个不同的变量数组: 第一个数组是多维的,另外两个是一维的。 我必须获得多维数组的最低值以及获得该值的人的名字。

您不知道的是如何关联两个不同变量的数组。

enter image description here

    $notas=array(array(5, 7.5, 6), 
             array(9, 6.5, 8), 
             array(8, 5.5, 7.5), 
             array(6.5, 5, 4));

$asignaturas=array("so", "hw", "html");
$nombres=array("Pepe", "Juan", "Ana", "Miren");



echo "<h4>The lowest note in all asginaturas and the name of the student who has earned </h4>";

for ($i=0; $i<=3; $i++)
{
    $array[$i]=min($notas[$i]);
}
$menor= min($array[0], $array[1], $array[2], $array[3]);
echo $menor;

到目前为止,我只收到了多维数组的最小值。

提前致谢。

2 个答案:

答案 0 :(得分:0)

我认为你应该使用一个多维数组:

$notas = array('Pepe' => array("so" => 5, "hw" => 7.5, "html" => 6), 
    'Juan' => array("so" => 9, "hw" => 6.5, "html" => 8), 
    'Ana' => array("so" => 8, "hw" => 5.5, "html" => 7.5), 
    'Miren' => array("so" => 6.5, "hw" => 5, "html" => 4));

$low_name = '';
$low_st = '';
$low_point = 0;
foreach ($notas as $name => $points) {
    if ('' == $low_name)
        $low_name = $name;
    foreach ($points as $k => $value) {
        if ('' == $low_st)
            $low_st = $k;
        if (0 == $low_point)
            $low_point = $value;

        if ($value < $low_point) {
            $low_point = $value;
            $low_st = $k;
            $low_name = $name;
        }
    }
}
echo $low_name . ' got ' . $low_point . ' in ' . $low_st;
// result is "Miren got 4 in html"

答案 1 :(得分:0)

$notas=array(array(5, 7.5, 6), 
             array(9, 6.5, 8), 
             array(8, 5.5, 7.5), 
             array(6.5, 5, 4));

$asignaturas=array("so", "hw", "html");
$nombres=array("Pepe", "Juan", "Ana", "Miren");



echo "<h4>The lowest note in all asginaturas and the name of the student who has earned </h4>";

for ($i=0; $i<=3; $i++)
{
    $array[$i]=min($notas[$i]);
}
$menor= min($array[0], $array[1], $array[2], $array[3]);
$names=array();                    // in case  multi  winners
for ($i=0; $i<=3; $i++)
{
   if( $menor==$array[$i])
   {
     $names[]= $nombres[i];
   }
}
foreach($names as $winner)
{
   echo $winner."<br/>";
}