0

I have a php array that is output to a table. But I need to sort the table by the first column, the second, third and fourth. Im not sure if I should use a function or ksort. What I have is below. I also seem to have some Notice: Use of undefined constants when I run it.

<!DOCTYPE html>
<html>
<body>
<?php
$state=array
(
array('Alabama', 'Montgomery', 4779736, 23),
array('Alaska', 'Juneau', 710231, 47),
array('Arizona', 'Phoenix', 6329017, 18),
array('Arkansas', 'LittleRock', 2915918, 32),
array('California', 'Sacramento', 37253956, 1),
array('Colorado', 'Denver', 5029196, 22),
array('Connecticut', 'Hartford', 3518288, 29),
array('Delaware', 'Dover', 897934, 45),
array('Florida', 'Tallahassee', 18801310, 4),
array('Georgia', 'Atlanta', 9687653, 9),
array('Hawaii', 'Boise', 1360301, 42)
);

echo "<table border=\"5\" cellpadding=\"10\">";
echo'<tr>';
   echo('<th>' . State. '</th>');
   echo('<th>' . Capital. '</th>');
   echo('<th>' . Population. '</th>');
   echo('<th>' . Rank. '</th>');
 echo'</tr>';

for ($i=0; $i<11; $i++)
{echo('<tr>');
  for ($j=0; $j<4; $j++)
  {echo ('<td>' . $state[$i][$j] . '</td>');
  }
  echo('</tr>');
}
echo "</table>"

?>
</body>
</html>

This is my other try at the sort:

<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
    </head>
    <body>
        <?php
$states[] = array('state' => 'Georgia', 'capital' => 'Atlanta', 'rank' => 9);
$states[] = array('state' => 'Alaska', 'capital' => 'Juneau', 'rank' => 47);
$states[] = array('state' => 'Alabama', 'capital' => 'Montgomery', 'rank' => 23);
$states[] = array('state' => 'Hawaii', 'capital' => 'Boise', 'rank' => 42);

$capital = array();
foreach ($states as $key => $row)
{
    $capital[$key] = $row['capital'];
}
echo \array_multisort($capital, \SORT_DESC, $states);

?>
    </body>
</html>

This is what I have so far, but for some reason, the numbered columns (population and rank) arent sorting correctly in their tables:

<?php 
$states=array
(
array('state' =>  'Alabama', 'capital' => 'Montgomery', 'population' => 4779736, 'rank' =>  23),
array('state' => 'Alaska', 'capital' =>  'Juneau', 'population' => 710231, 'rank' =>  47),
array('state' => 'Arizona',  'capital' => 'Phoenix',  'population' =>6329017, 'rank' =>  18),
array('state' => 'Arkansas',  'capital' => 'LittleRock',  'population' =>2915918, 'rank' =>  32),
array('state' => 'California', 'capital' =>  'Sacramento', 'population' => 37253956, 'rank' =>  1),
array('state' => 'Colorado',  'capital' => 'Denver', 'population' => 5029196,  'rank' => 22),
array('state' => 'Connecticut', 'capital' =>  'Hartford', 'population' => 3518288, 'rank' =>  29),
array('state' => 'Delaware',  'capital' => 'Dover',  'population' =>897934, 'rank' =>  45),
array('state' => 'Florida',  'capital' => 'Tallahassee', 'population' => 18801310, 'rank' =>  4),
array('state' => 'Georgia', 'capital' =>  'Atlanta', 'population' => 9687653,  'rank' => 9),
array('state' => 'Hawaii',  'capital' => 'Boise', 'population' => 1360301, 'rank' =>  42)
);


//Create index rows
foreach ($states as $row) {
  foreach ($row as $key => $value){
    ${$key}[]  = $value; //Creates $volume, $edition, $name and $type arrays.
  }  
}
echo '<h2>Below is the initial table</h2>';
echo "<table border=\"5\" cellpadding=\"10\">";
echo'<tr>';
   echo('<th> State </th>');
   echo('<th> Capital </th>');
   echo('<th> Population </th>');
   echo('<th> Rank </th>');
 echo'</tr>';
  foreach($states as $k => $val){ 
    echo "<tr> <td>".$val['state']."</td>
    <td>".$val['capital']."</td>
    <td>".$val['population']."</td>
    <td>".$val['rank']."</td></tr>  ";
  }
echo "</table>";


echo '<h2>Below is the State (first column) sort table - Ascending</h2>';
echo "<table border=\"5\" cellpadding=\"10\">";
echo'<tr>';
   echo('<th> State </th>');
   echo('<th> Capital </th>');
   echo('<th> Population </th>');
   echo('<th> Rank </th>');
 echo'</tr>';
 \array_multisort($state, \SORT_ASC, $states);
 foreach($states as $k => $val){ 
    echo "<tr> <td>".$val['state']."</td>
    <td>".$val['capital']."</td>
    <td>".$val['population']."</td>
    <td>".$val['rank']."</td></tr>  ";
  }
echo "</table>";


echo '<h2>Below is the Capital (second column) sort table - Descending</h2>';
echo "<table border=\"5\" cellpadding=\"10\">";
echo'<tr>';
   echo('<th> State </th>');
   echo('<th> Capital </th>');
   echo('<th> Population </th>');
   echo('<th> Rank </th>');
 echo'</tr>';
 \array_multisort($capital, \SORT_DESC, $states);
 foreach($states as $k => $val){ 
    echo "<tr> <td>".$val['state']."</td>
    <td>".$val['capital']."</td>
    <td>".$val['population']."</td>
    <td>".$val['rank']."</td></tr>  ";
  }
echo "</table>";


echo '<h2>Below is the Population (third column) sort table - Ascending</h2>';
echo "<table border=\"5\" cellpadding=\"10\">";
echo'<tr>';
   echo('<th> State </th>');
   echo('<th> Capital </th>');
   echo('<th> Population </th>');
   echo('<th> Rank </th>');
 echo'</tr>';
 \array_multisort($population, \SORT_ASC, $states);
 foreach($states as $k => $val){ 
    echo "<tr> <td>".$val['state']."</td>
    <td>".$val['capital']."</td>
    <td>".$val['population']."</td>
    <td>".$val['rank']."</td></tr>  ";
  }
echo "</table>";


echo '<h2>Below is the Rank (fourth column) sort table - Descending</h2>';
echo "<table border=\"5\" cellpadding=\"10\">";
echo'<tr>';
   echo('<th> State </th>');
   echo('<th> Capital </th>');
   echo('<th> Population </th>');
   echo('<th> Rank </th>');
 echo'</tr>';
 \array_multisort($rank, \SORT_DESC, $states);
 foreach($states as $k => $val){ 
    echo "<tr> <td>".$val['state']."</td>
    <td>".$val['capital']."</td>
    <td>".$val['population']."</td>
    <td>".$val['rank']."</td></tr>  ";
  }
echo "</table>";
?>
</body>
</html>
9
  • It might even be OP's PHP version. Mine is 5.4.23 @Jack Commented Mar 10, 2014 at 3:14
  • checkthis link [How to sort associative array using sub-field of contained associative arrays in PHP? Commented Mar 10, 2014 at 3:14
  • Can I sort by each column with the original array - without using a key? Or should I use the second array syntax? And how do I echo the sorted table? Commented Mar 10, 2014 at 3:19
  • If you are grabbing this array values from database, it would be really good to bring sorted results based on query as per required, that way you can sort each column ASC/DESC as required Commented Mar 10, 2014 at 3:21
  • This is not from a database. Commented Mar 10, 2014 at 3:23

1 Answer 1

4

Use this code , i think this is what you want ..

<?php 
$states=array
(
array('state' =>  'Alabama', 'capital' => 'Montgomery', 'population' => 4779736, 'rank' =>  23),
array('state' => 'Alaska', 'capital' =>  'Juneau', 'population' => 710231, 'rank' =>  47),
array('state' => 'Arizona',  'capital' => 'Phoenix',  'population' =>6329017, 'rank' =>  18),
array('state' => 'Arkansas',  'capital' => 'LittleRock',  'population' =>2915918, 'rank' =>  32),
array('state' => 'California', 'capital' =>  'Sacramento', 'population' => 37253956, 'rank' =>  1),
array('state' => 'Colorado',  'capital' => 'Denver', 'population' => 5029196,  'rank' => 22),
array('state' => 'Connecticut', 'capital' =>  'Hartford', 'population' => 3518288, 'rank' =>  29),
array('state' => 'Delaware',  'capital' => 'Dover',  'population' =>897934, 'rank' =>  45),
array('state' => 'Florida',  'capital' => 'Tallahassee', 'population' => 18801310, 'rank' =>  4),
array('state' => 'Georgia', 'capital' =>  'Atlanta', 'population' => 9687653,  'rank' => 9),
array('state' => 'Hawaii',  'capital' => 'Boise', 'population' => 1360301, 'rank' =>  42)
);


//Create index rows
foreach ($states as $row) {
  foreach ($row as $key => $value){
    ${$key}[]  = $value; //Creates $volume, $edition, $name and $type arrays.
  }  
}
array_multisort($state, SORT_ASC, $capital, SORT_ASC, $population, SORT_ASC, $rank , SORT_ASC,  $states);

echo "<table border=\"5\" cellpadding=\"10\">";
echo'<tr>';
   echo('<th> State </th>');
   echo('<th> Capital </th>');
   echo('<th> Population </th>');
   echo('<th> Rank </th>');
 echo'</tr>';

 foreach($states as $k => $val){ 

    echo "<tr> <td>".$val['state']."</td>
    <td>".$val['capital']."</td>
    <td>".$val['population']."</td>
    <td>".$val['rank']."</td></tr>  ";

  }

echo "</table>"

?>
</body>
</html>
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you. That got rid of the warning messages. But I still am not sure on how to sort. Does this array syntax make it harder to sort each column?
TheoneNOnlyQ , i have updated my answer above check it
Yes, thank you. Thats one sort. But I need to sort by the other 3 columns also.
OK I see this will sort by capital \array_multisort($capital, \SORT_ASC, $states); But i need a loop Im guessing, to get the others without retyping it all over again???

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.