Sort associative array












0















This has been asked before but the examples given have not worked for me.



I have an array with two keys



$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),
);


And if I go through the array I get the output in the original order



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


I need them to be sorted ascending according to the key 'valor' so i tried using usort



usort($miarray, 'sort_valor');


with the following function I found in the examples given.



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

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


but I get the following error




[20-Nov-2018 19:12:08 UTC] PHP Warning: usort() expects parameter 2
to be a valid callback, function 'sort_valor' not found or invalid
function name in xxxxxxxxxxx




the following two files i use



<?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>


and



<?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);
?>









share|improve this question




















  • 1





    Can you post your full code? I can't reproduce your problem.

    – ggorlen
    Nov 20 '18 at 19:46











  • @FatemehNB PHP will hoist the comparator function. It shouldn't be an issue here.

    – ggorlen
    Nov 20 '18 at 19:47






  • 1





    Is this code running within a class?

    – Patrick Q
    Nov 20 '18 at 19:51






  • 3





    You have sort_valor in your JavaScript code, not PHP.

    – Barmar
    Nov 20 '18 at 19:58






  • 1





    BTW, you can simplify it to just function sort_valor($a, $b) { return $a['valor'] - $b['valor']); }

    – Barmar
    Nov 20 '18 at 19:59
















0















This has been asked before but the examples given have not worked for me.



I have an array with two keys



$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),
);


And if I go through the array I get the output in the original order



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


I need them to be sorted ascending according to the key 'valor' so i tried using usort



usort($miarray, 'sort_valor');


with the following function I found in the examples given.



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

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


but I get the following error




[20-Nov-2018 19:12:08 UTC] PHP Warning: usort() expects parameter 2
to be a valid callback, function 'sort_valor' not found or invalid
function name in xxxxxxxxxxx




the following two files i use



<?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>


and



<?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);
?>









share|improve this question




















  • 1





    Can you post your full code? I can't reproduce your problem.

    – ggorlen
    Nov 20 '18 at 19:46











  • @FatemehNB PHP will hoist the comparator function. It shouldn't be an issue here.

    – ggorlen
    Nov 20 '18 at 19:47






  • 1





    Is this code running within a class?

    – Patrick Q
    Nov 20 '18 at 19:51






  • 3





    You have sort_valor in your JavaScript code, not PHP.

    – Barmar
    Nov 20 '18 at 19:58






  • 1





    BTW, you can simplify it to just function sort_valor($a, $b) { return $a['valor'] - $b['valor']); }

    – Barmar
    Nov 20 '18 at 19:59














0












0








0








This has been asked before but the examples given have not worked for me.



I have an array with two keys



$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),
);


And if I go through the array I get the output in the original order



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


I need them to be sorted ascending according to the key 'valor' so i tried using usort



usort($miarray, 'sort_valor');


with the following function I found in the examples given.



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

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


but I get the following error




[20-Nov-2018 19:12:08 UTC] PHP Warning: usort() expects parameter 2
to be a valid callback, function 'sort_valor' not found or invalid
function name in xxxxxxxxxxx




the following two files i use



<?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>


and



<?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);
?>









share|improve this question
















This has been asked before but the examples given have not worked for me.



I have an array with two keys



$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),
);


And if I go through the array I get the output in the original order



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


I need them to be sorted ascending according to the key 'valor' so i tried using usort



usort($miarray, 'sort_valor');


with the following function I found in the examples given.



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

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


but I get the following error




[20-Nov-2018 19:12:08 UTC] PHP Warning: usort() expects parameter 2
to be a valid callback, function 'sort_valor' not found or invalid
function name in xxxxxxxxxxx




the following two files i use



<?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>


and



<?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);
?>






php arrays sorting associative-array






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 '18 at 19:56







Ingolf Krauss

















asked Nov 20 '18 at 19:42









Ingolf KraussIngolf Krauss

581111




581111








  • 1





    Can you post your full code? I can't reproduce your problem.

    – ggorlen
    Nov 20 '18 at 19:46











  • @FatemehNB PHP will hoist the comparator function. It shouldn't be an issue here.

    – ggorlen
    Nov 20 '18 at 19:47






  • 1





    Is this code running within a class?

    – Patrick Q
    Nov 20 '18 at 19:51






  • 3





    You have sort_valor in your JavaScript code, not PHP.

    – Barmar
    Nov 20 '18 at 19:58






  • 1





    BTW, you can simplify it to just function sort_valor($a, $b) { return $a['valor'] - $b['valor']); }

    – Barmar
    Nov 20 '18 at 19:59














  • 1





    Can you post your full code? I can't reproduce your problem.

    – ggorlen
    Nov 20 '18 at 19:46











  • @FatemehNB PHP will hoist the comparator function. It shouldn't be an issue here.

    – ggorlen
    Nov 20 '18 at 19:47






  • 1





    Is this code running within a class?

    – Patrick Q
    Nov 20 '18 at 19:51






  • 3





    You have sort_valor in your JavaScript code, not PHP.

    – Barmar
    Nov 20 '18 at 19:58






  • 1





    BTW, you can simplify it to just function sort_valor($a, $b) { return $a['valor'] - $b['valor']); }

    – Barmar
    Nov 20 '18 at 19:59








1




1





Can you post your full code? I can't reproduce your problem.

– ggorlen
Nov 20 '18 at 19:46





Can you post your full code? I can't reproduce your problem.

– ggorlen
Nov 20 '18 at 19:46













@FatemehNB PHP will hoist the comparator function. It shouldn't be an issue here.

– ggorlen
Nov 20 '18 at 19:47





@FatemehNB PHP will hoist the comparator function. It shouldn't be an issue here.

– ggorlen
Nov 20 '18 at 19:47




1




1





Is this code running within a class?

– Patrick Q
Nov 20 '18 at 19:51





Is this code running within a class?

– Patrick Q
Nov 20 '18 at 19:51




3




3





You have sort_valor in your JavaScript code, not PHP.

– Barmar
Nov 20 '18 at 19:58





You have sort_valor in your JavaScript code, not PHP.

– Barmar
Nov 20 '18 at 19:58




1




1





BTW, you can simplify it to just function sort_valor($a, $b) { return $a['valor'] - $b['valor']); }

– Barmar
Nov 20 '18 at 19:59





BTW, you can simplify it to just function sort_valor($a, $b) { return $a['valor'] - $b['valor']); }

– Barmar
Nov 20 '18 at 19:59












1 Answer
1






active

oldest

votes


















0














The problem is your sort function is in the JavaScript portion of your code. Move it below the <?php tag in the file you'd like to reference it in (or include/require it from another PHP file).






share|improve this answer

























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53400421%2fsort-associative-array%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    The problem is your sort function is in the JavaScript portion of your code. Move it below the <?php tag in the file you'd like to reference it in (or include/require it from another PHP file).






    share|improve this answer






























      0














      The problem is your sort function is in the JavaScript portion of your code. Move it below the <?php tag in the file you'd like to reference it in (or include/require it from another PHP file).






      share|improve this answer




























        0












        0








        0







        The problem is your sort function is in the JavaScript portion of your code. Move it below the <?php tag in the file you'd like to reference it in (or include/require it from another PHP file).






        share|improve this answer















        The problem is your sort function is in the JavaScript portion of your code. Move it below the <?php tag in the file you'd like to reference it in (or include/require it from another PHP file).







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 20 '18 at 20:00

























        answered Nov 20 '18 at 19:58









        ggorlenggorlen

        7,1883825




        7,1883825






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53400421%2fsort-associative-array%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            'app-layout' is not a known element: how to share Component with different Modules

            android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

            WPF add header to Image with URL pettitions [duplicate]