我正在尝试对多维数组进行排序。该阵列包含City&人口。它们显示在一个表中。我想按升序对Population列进行排序。我明白,程序是sort($array_variable)
,但我不知道如何在排序后打印它们。请帮我教育一下这件事。该计划如下
<html>
<head>
<style>
table, tr, td, th {
border:1px solid blue;
border-collapse: collapse;
}
</style>
</head>
<body>
<?php
$data=array (
array('New York','Washington',8008278),
array('Los Angeles','California',3694820),
array('Chicago','Illinois',2896016),
array('Houston','Texas',1953631),
array('Philadelphia','Philadelphia',1517550),
array('Pheonix','Arizona',1321045),
array('San Diego','California',1223400),
array('Dallas','Texas',188580),
array('San Antonio','Texas',144646),
array('Detroit','Michigan',951270),
);
print '<table>';
print '<th>'; print "City"; print '</th>';
print '<th>'; print "State"; print '</th>';
print '<th>'; print "Population"; print '</th>';
print '<tr>';
print '<td>'; print $data[0][0]; print '</td>';
print '<td>'; print $data[0][1]; print '</td>';
print '<td>'; print $data[0][2]; print '</td>';
print '</tr>';
print '<tr>';
print '<td>'; print $data[1][0]; print '</td>';
print '<td>'; print $data[1][1]; print '</td>';
print '<td>'; print $data[1][2]; print '</td>';
print '</tr>';
print '<tr>';
print '<td>'; print $data[2][0]; print '</td>';
print '<td>'; print $data[2][1]; print '</td>';
print '<td>'; print $data[2][2]; print '</td>';
print '</tr>';
print '<tr>';
print '<td>'; print $data[3][0]; print '</td>';
print '<td>'; print $data[3][1]; print '</td>';
print '<td>'; print $data[3][2]; print '</td>';
print '</tr>';
print '<tr>';
print '<td>'; print $data[4][0]; print '</td>';
print '<td>'; print $data[4][1]; print '</td>';
print '<td>'; print $data[4][2]; print '</td>';
print '</tr>';
print '<tr>';
print '<td>'; print $data[5][0]; print '</td>';
print '<td>'; print $data[5][1]; print '</td>';
print '<td>'; print $data[5][2]; print '</td>';
print '</tr>';
print '<tr>';
print '<td>'; print $data[6][0]; print '</td>';
print '<td>'; print $data[6][1]; print '</td>';
print '<td>'; print $data[6][2]; print '</td>';
print '</tr>';
print '<tr>';
print '<td>'; print $data[7][0]; print '</td>';
print '<td>'; print $data[7][1]; print '</td>';
print '<td>'; print $data[7][2]; print '</td>';
print '</tr>';
print '<tr>';
print '<td>'; print $data[8][0]; print '</td>';
print '<td>'; print $data[8][1]; print '</td>';
print '<td>'; print $data[8][2]; print '</td>';
print '</tr>';
print '<tr>';
print '<td>'; print $data[9][0]; print '</td>';
print '<td>'; print $data[9][1]; print '</td>';
print '<td>'; print $data[9][2]; print '</td>';
print '</tr>';
print '<tr>';
print '<td colspan=2>'; print "Total Population is:";
print '</td>';
print '<td>'; print $data[0][2]+$data[1][2]+$data[2][2]+$data[3][2]+$data[4][2]+$data[5][2]+$data[6][2]+$data[7][2]+$data[8][2]+$data[9][2]; print '</td>';
print '</tr>';
print '<tr>';
print '<td>'; print sort($data); print '</td>';
print '</tr>';
print '</table>';
?>
</body>
很抱歉这个长计划,这里有一个菜鸟。正如你看到的。我已经给出了sort()
函数,但它没有给出所需的输出。谢谢。
答案 0 :(得分:2)
只需使用循环打印出数据即可。我还要创建一个类City
并使用usort
根据population
属性进行比较。像这样:
<?php
class City{
public $population;
public $name;
public $state;
public function __construct($name,$state,$population) {
$this->name = $name;
$this->state = $state;
$this->population = $population;
}
}
$data=array (
new City('New York','Washington',8008278),
new City('Los Angeles','California',3694820),
new City('Chicago','Illinois',2896016),
new City('Houston','Texas',1953631),
new City('Philadelphia','Philadelphia',1517550),
new City('Pheonix','Arizona',1321045),
new City('San Diego','California',1223400),
new City('Dallas','Texas',188580),
new City('San Antonio','Texas',144646),
new City('Detroit','Michigan',951270)
);
usort($data, function($a, $b)
{
return $a->population < $b->population;
});
print '<table>';
print '<tr>';
print '<th>'; print "City"; print '</th>';
print '<th>'; print "State"; print '</th>';
print '<th>'; print "Population"; print '</th>';
print '</tr>';
foreach($data as $currCity){
print '<tr>';
print '<td>'; print $currCity->name; print '</td>';
print '<td>'; print $currCity->state; print '</td>';
print '<td>'; print $currCity->population; print '</td>';
print '</tr>';
}
答案 1 :(得分:1)
尝试:
<table>
<tr>
<th> City </th>
<th> State </th>
<th> Population </th>
</tr>
<?php
$totalPop = 0;
foreach($data as $d) { ?>
<tr>
<th> <?php echo $d[0]; ?> </th>
<th> <?php echo $d[1]; ?> </th>
<th> <?php echo $d[2]; $totalPop += $d[2]; ?> </th>
</tr>
<?php } ?>
<tr>
<td colspan="2"> Total Population is: </td>
<td><?php echo $totalPop; ?></td>
</tr>
</table>
答案 2 :(得分:1)
请尝试
<?php
$data = array(
array('New York', 'Washington', 8008278),
array('Los Angeles', 'California', 3694820),
array('Chicago', 'Illinois', 2896016),
array('Houston', 'Texas', 1953631),
array('Philadelphia', 'Philadelphia', 1517550),
array('Pheonix', 'Arizona', 1321045),
array('San Diego', 'California', 1223400),
array('Dallas', 'Texas', 188580),
array('San Antonio', 'Texas', 144646),
array('Detroit', 'Michigan', 951270),
);
usort($data, function($a, $b) {
return $a[2] - $b[2];
});
?>
<table>
<tr>
<th>City</th>
<th>State</th>
<th>Population</th>
</tr>
<?php
$totalPopulation=0;
foreach($data as $d)
{
?>
<tr>
<td><?php echo $d[0];?></td>
<td><?php echo $d[1];?></td>
<td><?php echo $d[2];?></td>
</tr>
<?php
$totalPopulation = $totalPopulation+$d[2];
}
?>
<tr><td>Total Population</td><td colspan="2"><?php echo $totalPopulation; ?></td></tr>
</table>