排序关联数组

时间:2018-11-20 19:42:23

标签: php arrays sorting associative-array

以前曾有人问过这个问题,但给出的例子对我没有用。

我有一个带有两个键的数组

$miarray = array(
    array('factor' => $textof[1],    'valor' => $i1),
    array('factor' => $textof[2],    'valor' => $i2),
    array('factor' => $textof[3],    'valor' => $i3),
    array('factor' => $textof[4],    'valor' => $i4),
    array('factor' => $textof[5],    'valor' => $i5),
    array('factor' => $textof[6],    'valor' => $i6),
    array('factor' => $textof[7],    'valor' => $i7),
    array('factor' => $textof[8],    'valor' => $i8),
    array('factor' => $textof[9],    'valor' => $i9),
    array('factor' => $textof[10],    'valor' => $i10),
);

如果我遍历数组,则会按原始顺序获得输出

foreach ($miarray as $optionArray){ 
$pr .= "$optionArray[factor] - $optionArray[valor]<br>";
}

我需要根据键“ valor”对它们进行升序排序,所以我尝试使用usort

usort($miarray, 'sort_valor');

具有以下示例中提供的以下功能。

function sort_valor($a, $b)
{
    $a = $a['valor'];
    $b = $b['valor'];

    if ($a == $b) return 0;
    return ($a < $b) ? -1 : 1;
}

但出现以下错误

  

[2018年11月20日19:12:08 UTC] PHP警告:usort()需要参数2   是有效的回调,未找到函数'sort_valor'或无效   xxxxxxxxxxx中的函数名称

我使用以下两个文件

<?php include("config.php"); ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" dir="ltr" lang="en-US">

<head profile="http://gmpg.org/xfn/11">
<meta http-equiv="Content-Type" content="text/html; charset="iso-8859-1" />

<title>::: Clima :::</title>

<link rel="stylesheet" href="style2.css" type="text/css" media="screen" />
<link rel="stylesheet" type="text/css" href="hsd-flotr2.css" />

<link rel="icon" href="images/favicon.ico" />
<link rel="shortcut icon" href="images/favicon.ico" />

<script type="text/javascript" src="jquery-1.7.1.js"></script>
<script type="text/javascript" src="jquery.form.js"></script>

<script type="text/javascript" >
$(document).ready(function() {
        $('#encuesta').ajaxForm({
            target: '#preview',
            success: function() {
            $('#preview').fadeIn('slow');
            }
        });
        $("#encuesta").submit();
    });

function sort_valor($a, $b)
{
    $a = $a['valor'];
    $b = $b['valor'];

    if ($a == $b) return 0;
    return ($a < $b) ? -1 : 1;
}
</script>

</head>
<body>

<?php


include("header.php");
include("footer.php");

$db = mysql_connect($server,$user,$pass);
mysql_select_db($dbname,$db);


echo"<div id='page' class='clearfloat'>";
echo"<div id='sidebar'></div>";

echo"<div id='content' class='Absolute-Center'>";


//_______________________________resultados

echo"<div id='preview' style='margin-left:50px; height:530px;overflow:auto; float:left;'></div>";

//_______________________________formulario
echo"<div STYLE='float:left;'>";
echo"<form id='encuesta' method='post' action='analizar_3.php'>";
//_______________________________filtros

$f=1;
echo"<table>";
$result_3=mysql_query("SELECT DISTINCT tipo1 FROM filtros;");
while($row_3 = mysql_fetch_array($result_3)) {
echo"<tr><td>$row_3[0]</td></tr><tr><td><select id=f$f name=f$f>";

echo"<option value='0'>Todos</option>";

$result_4=mysql_query("SELECT id_filtros,tipo2 FROM filtros WHERE tipo1 = '$row_3[0]';");
while($row_4 = mysql_fetch_array($result_4)) {
$tt = substr($row_4[1],0,40);
echo"<option value='$row_4[0]'>$tt</option>";
}
echo"</select></td></tr>";
$f++;
}
echo"</table>";
//_______________________________filtros

echo"<input type=submit id='submit' value='Enviar'>";

echo"</form></div>";

echo"</div>";
echo"</div>";

mysql_close($db);
?>
</body>
</html>

<?php include("config.php"); ?>
<?php include("Includes/FusionCharts.php"); ?>
<?php


$db = mysql_connect($server,$user,$pass);
mysql_select_db($dbname,$db);

$pr = "";

$result_11=mysql_query("SELECT DISTINCT tipo1 FROM filtros;");
$za=1;
while($row_11 = mysql_fetch_array($result_11)) {
$filtro[$za] = $row_11[0];
$za++;
}

$result_1=mysql_query("SELECT DISTINCT tipo1 FROM filtros;");
$row_1 = mysql_num_rows($result_1);

for ($x=1; $x<=$row_1; $x++)
{
$aa = 'f'.$x;
$f[$x] = $_POST[$aa];

$total = $total + $f[$x];
}

$sql2 = "";

for ($x=1; $x<=$row_1; $x++)
{
$xx = $x + 1;
if ($f[$x] > 0) {$sql2 .= "AND $filtro[$x] = $f[$x] ";}

}

//____________________________________________________limite de afirmaciones por factor

$a=0;
$b=0;
$i=1;

$result_2=mysql_query("SELECT DISTINCT tipo1 FROM encuesta;");
while($row_2 = mysql_fetch_array($result_2)) {

$result_3=mysql_query("SELECT count(*) FROM encuesta WHERE tipo1 = '$row_2[0]';");
$row_3 = mysql_fetch_array($result_3);
$a = $b + 1;
$b = $a + $row_3[0] - 1;

$lim1[$i] = $a;
$lim2[$i] = $b;
$i++;
}

//____________________________________________________suma por pregunta

$result_4=mysql_query("SELECT DISTINCT hash FROM resultados2 WHERE id_resultados2 > 0 $sql2;");
$row_4 = mysql_num_rows($result_4);

$result_5=mysql_query("SELECT DISTINCT id_encuesta FROM encuesta;");
while($row_5 = mysql_fetch_array($result_5)) {

$j = $row_5[0];
$k ='a';
$kj = $k.$j;

$result_6=mysql_query("SELECT sum($kj) FROM resultados2 WHERE $kj != 6 $sql2;");

$row_6 = mysql_fetch_array($result_6);
$p[$j] = $row_6[0];

$result_7=mysql_query("SELECT count($kj) FROM resultados2 WHERE $kj = 6 $sql2;");
$row_7 = mysql_fetch_array($result_7);

$dif = $row_4 - $row_7[0];
if($dif == 0) {$dif = 1;}

$valafi[$j] = $p[$j] / $dif;

}

//____________________________________________________suma por factor


$result_11=mysql_query("SELECT DISTINCT tipo1 FROM encuesta;");
$row_11 = mysql_num_rows($result_11);

for ($x=1; $x<=$row_11; $x++)
{

for ($y=$lim1[$x]; $y<=$lim2[$x]; $y++)
{
$valfactor[$x] = $valfactor[$x] + $valafi[$y];
}

$valfactor[$x] = $valfactor[$x] / ($lim2[$x] - $lim1[$x] + 1);
}

//____________________________________________________indicador general

for ($x=1; $x<=$row_11; $x++)
{
$ind = $ind + $valfactor[$x];
}
if ($ind > 0) {$ind = $ind / 10; $ind = round($ind,2);}

//____________________________________________________grafico resumen

$result_10=mysql_query("SELECT * FROM factores;");
while($row_10 = mysql_fetch_array($result_10)) {
$f=$row_10[0];
$textof[$f] = $row_10[1];
}

$result_20 = mysql_query("SELECT * FROM resultados3 WHERE id_resultados3 > 0 $sql2;");
while($row_20 = mysql_fetch_array($result_20)) {

$multi = 100/10;
$startwert = 11;

$i1 = $i1 + (($startwert - $row_20[9]) * $multi);
$i2 = $i2 + (($startwert - $row_20[10]) * $multi);
$i3 = $i3 + (($startwert - $row_20[11]) * $multi);
$i4 = $i4 + (($startwert - $row_20[12]) * $multi);
$i5 = $i5 + (($startwert - $row_20[13]) * $multi);
$i6 = $i6 + (($startwert - $row_20[14]) * $multi);
$i7 = $i7 + (($startwert - $row_20[15]) * $multi);
$i8 = $i8 + (($startwert - $row_20[16]) * $multi);
$i9 = $i9 + (($startwert - $row_20[17]) * $multi);
$i10 = $i10 + (($startwert - $row_20[18]) * $multi);

}

$result_21 = mysql_query("SELECT * FROM resultados3 WHERE id_resultados3 > 0 $sql2;");
$row_21 = mysql_num_rows($result_21);

$i1 = $i1 / $row_21;
$i2 = $i2 / $row_21;
$i3 = $i3 / $row_21;
$i4 = $i4 / $row_21;
$i5 = $i5 / $row_21;
$i6 = $i6 / $row_21;
$i7 = $i7 / $row_21;
$i8 = $i8 / $row_21;
$i9 = $i9 / $row_21;
$i10 = $i10 / $row_21;

$i1 = round($i1,2);
$i2 = round($i2,2);
$i3 = round($i3,2);
$i4 = round($i4,2);
$i5 = round($i5,2);
$i6 = round($i6,2);
$i7 = round($i7,2);
$i8 = round($i8,2);
$i9 = round($i9,2);
$i10 = round($i10,2);

$miarray = array(
    array('factor' => $textof[1],    'valor' => $i1),
    array('factor' => $textof[2],    'valor' => $i2),
    array('factor' => $textof[3],    'valor' => $i3),
    array('factor' => $textof[4],    'valor' => $i4),
    array('factor' => $textof[5],    'valor' => $i5),
    array('factor' => $textof[6],    'valor' => $i6),
    array('factor' => $textof[7],    'valor' => $i7),
    array('factor' => $textof[8],    'valor' => $i8),
    array('factor' => $textof[9],    'valor' => $i9),
    array('factor' => $textof[10],    'valor' => $i10),
);

usort($miarray, 'sort_valor');

foreach ($miarray as $optionArray){ 
$pr .= "$optionArray[factor] - $optionArray[valor]<br>";
}

$pr .= "<a name='subir'></a><table><tr><td><label STYLE='font-size:24px;'>Resultados por Factores</label></td><tr>";
$pr .= "<tr><td>Número de encuestados afectos: $row_4</td></tr>";
if($row_4 >= 5) { 
$strXML  = "";
$strXML .= "<graph xAxisName='Factores' yAxisName='Evaluacion de los Factores' decimalPrecision='2' formatNumberScale='0' canvasBorderThickness='1' canvasBorderColor='114B78' showBarShadow='1' yAxisMaxValue='100' chartRightMargin='10' >";

for ($x=1; $x<=$row_11; $x++)
{
if($valfactor[$x] <= 60) {$colorf[$x] = "FF3100";} else {$colorf[$x] = "0031FF";}

$strXML .= "<set link='#ancla$x' name='$textof[$x]' value='$valfactor[$x]' color='$colorf[$x]' />";

}


$strXML .= "</graph>";
$pr .= "<tr><td>";
$pr .= renderChartHTML("FusionCharts/FCF_Bar2D.swf", "", $strXML, "myNext", 850, 420);
$pr .= "</td></tr>";

$pr .="<tr><td><div STYLE='width: 100%;height: 47px;border-bottom: 1px solid black;'></div></td></tr>";

$pr .="<tr><td><table>";
$pr .= "<tr><td><label STYLE='font-size:24px;'>El indicador de clima laboral es $ind</label></td></tr>";
$pr .= "<tr><td><br><br></td></tr>";

$pr .="</table></td></tr>";

//____________________________________________________Importancia
$pr .="<tr><td><div STYLE='width: 100%;height: 47px;border-bottom: 1px solid black;'></div></td></tr>";

$pr .= "<tr><td><table>";
$pr .= "<tr><td><label STYLE='font-size:24px;'>Importancia de los factores</label></td></tr>";
$pr .= "<tr><td>$textof[1] </td><td>$i1</td></tr>";
$pr .= "<tr><td>$textof[2]</td><td> $i2</td></tr>";
$pr .= "<tr><td>$textof[3] </td><td>$i3</td></tr>";
$pr .= "<tr><td>$textof[4] </td><td>$i4</td></tr>";
$pr .= "<tr><td>$textof[5] </td><td>$i5</td></tr>";
$pr .= "<tr><td>$textof[6] </td><td>$i6</td></tr>";
$pr .= "<tr><td>$textof[7] </td><td>$i7</td></tr>";
$pr .= "<tr><td>$textof[8] </td><td>$i8</td></tr>";
$pr .= "<tr><td>$textof[9] </td><td>$i9</td></tr>";
$pr .= "<tr><td>$textof[10] </td><td>$i10</td></tr>";
$pr .= "</table></td></tr>";

//____________________________________________________graficos por factor

$pr .="<tr><td><div STYLE='width: 100%;height: 47px;border-bottom: 1px solid black;'></div></td></tr>";

$result_9=mysql_query("SELECT * FROM encuesta;");
while($row_9 = mysql_fetch_array($result_9)) {
$f=$row_9[0];
$texto[$f] = $row_9[2];
}

$result_8=mysql_query("SELECT DISTINCT tipo1 FROM encuesta;");
$row_8 = mysql_num_rows($result_8);

for ($x=1; $x<=$row_8; $x++)
{
$pr .= "<tr><td STYLE='padding-top:25px;'><a name='ancla$x'></a>Factor $x: $textof[$x]</td></tr>";
$strXML  = "";
$strXML .= "<graph xAxisName='Afirmaciones' yAxisName='Evaluacion de los Factores' decimalPrecision='2' formatNumberScale='0' canvasBorderThickness='1' canvasBorderColor='114B78' showBarShadow='1' yAxisMaxValue='100' chartRightMargin='10' animation='0'>";

for ($y=$lim1[$x]; $y<=$lim2[$x]; $y++)
{
$gg = $y - $lim1[$x] +1;
if($valafi[$y] <= 60) {$color[$y] = "FF3100";} else {$color[$y] = "0031FF";}
$strXML .= "<set name='$gg' hoverText='$texto[$y]' value='$valafi[$y]' color='$color[$y]' />";
}
$height = 35 * ($lim2[$x] - $lim1[$x]);

if ($height < 350) {$height = 350;}

$strXML .= "</graph>";
$pr .= "<tr><td>";
$pr .= renderChartHTML("FusionCharts/FCF_Bar2D.swf", "", $strXML, "myNext", 850, $height);
$pr .= "</td></tr>";;
$pr .= "<tr><td><a href ='#subir'>Volver al resumen</a></td></tr>";
}

}

$pr .= "</table>";

$pr = utf8_encode ($pr);
echo $pr;

mysql_close($db);
?>

1 个答案:

答案 0 :(得分:0)

问题在于您的排序功能在代码的JavaScript部分中。将其移动到您要引用的文件中的<?php标记下(或从另一个PHP文件中将其include / require移到该标记下)。