如何在php中创建动态矩阵?

时间:2015-03-29 12:22:15

标签: php mysql

我的数据库中有这个表“city”:

|id |id_city_a |id_city_b|distance|
|1  |1         | 1   | 0      |
|2  |1         | 2   | 8      |
|3  |1         | 3   | 6      |
|4  |2         | 1   | 8      |
|5  |2         | 2   | 0      |
|6  |2         | 3   | 9      |
|7  |3         | 1   | 6      |
|8  |3         | 2   | 9      |
|9  |3         | 3   | 0      |

我希望最终结果在矩阵中,例如:

| | 1 | 2 | 3 | | 1 | 0 | 8 | 6 | | 2 | 8 | 0 | 9 | | 3 | 6 | 9 | 0 |

This is my code :

function random() { include('config/koneksi.php'); $result = mysql_query("select * from temp_hasil"); $n =mysql_num_rows(mysql_query("SELECT * FROM temp_hasil")); for ($i = 1; $i <= $n; $i++) { for ($j = 1; $j <= $n; $j++) { $rows = mysql_fetch_array($result); $this->table[$i][$j] = $i == $j ? INF : $rows['id']; } } } function __toString() { $str = '<table class="table table-bordered" id="tableInput"> <tbody>'; $str .= '<tr><td></td>'; foreach ($this->table as $rowName => $row) { $str .= "<td>$rowName</td>"; } $str .= '</tr>'; foreach ($this->table as $rowName => $row) { $str .= "<tr><td>$rowName</td>"; foreach ($row as $columnName => $value) { $str .= "<td>"; $str .= '<input class="form-control" type="text" value="' . $value . '" name="table[' . $rowName . '][' . $columnName . ']" requied' . ($columnName == $rowName ? ' disabled' : '') . '>'; $str .= "</td>"; } $str .= '</tr>'; } $str .= '</tbody></table>'; return $str; } } $str .= '</tr>'; foreach ($this->table as $rowName => $row) { $str .= "<tr><td>$rowName</td>"; foreach ($row as $columnName => $value) { $str .= "<td>"; $str .= '<input class="form-control" type="text" value="' . $value . '" name="table[' . $rowName . '][' . $columnName . ']" requied' . ($columnName == $rowName ? ' disabled' : '') . '>'; $str .= "</td>"; } $str .= '</tr>'; } $str .= '</tbody></table>'; return $str; }

`

我如何在PHP中编码?请帮帮我。

2 个答案:

答案 0 :(得分:1)

试试这个

Online Demo

$tableRows[0]=array("id"=>1,"id_city_a"=>1,"id_city_b"=>1,"distance"=>0);
$tableRows[1]=array("id"=>2,"id_city_a"=>1,"id_city_b"=>2,"distance"=>8);
$tableRows[2]=array("id"=>3,"id_city_a"=>1,"id_city_b"=>3,"distance"=>6);
$tableRows[3]=array("id"=>4,"id_city_a"=>2,"id_city_b"=>1,"distance"=>8);
$tableRows[4]=array("id"=>5,"id_city_a"=>2,"id_city_b"=>2,"distance"=>0);
$tableRows[5]=array("id"=>6,"id_city_a"=>2,"id_city_b"=>3,"distance"=>9);
$tableRows[6]=array("id"=>7,"id_city_a"=>3,"id_city_b"=>1,"distance"=>6);
$tableRows[7]=array("id"=>8,"id_city_a"=>3,"id_city_b"=>2,"distance"=>9);
$tableRows[8]=array("id"=>9,"id_city_a"=>3,"id_city_b"=>3,"distance"=>0);

$counter=0;
$result=array();
foreach($tableRows as $tableRow)
{
    $result[$tableRow["id_city_a"]][$tableRow["id_city_b"]]=array("id_city_a"=>$tableRow["id_city_a"],"id_city_b"=>$tableRow["id_city_b"],"distance"=>$tableRow["distance"] );
}

现在在$result[cityA][cityB]=distance

获取列

的unique_id_city_a
$unique_id_city_a = array_unique(array_map(function ($i) { return $i['id_city_a']; }, $tableRows));
echo "\t";
foreach($unique_id_city_a as $R)
{
    echo $R."\t";
}
echo "\n";

获取行的unique_id_city_b

$unique_id_city_b = array_unique(array_map(function ($i) { return $i['id_city_b']; }, $tableRows));


foreach($result as $R1)
{       
    echo $unique_id_city_b[$counter++]."\t";
    foreach($R1 as $R2)
    {
        echo $R2["distance"]."\t";
    }
    echo "\n";
}

答案 1 :(得分:0)

使city_a索引与city_b索引完全不同,因此很容易检查。

// generate a two-dimensional matrix in here
$distMatrix = array();

foreach($tableRows as $cityDist) {
    $from = $cityDist['id_city_a'];
    $to = $cityDist['id_city_b'];
    $dist = $cityDist['distance'];

    $distMatrix[$from][$to] = $dist;
}

显示为HTML表格......

echo '<table border="1">';
echo '<tr>';
echo '<td>', '#', '</td>';
foreach(array_keys(current($distMatrix)) as $city_b) { // city_b headings
   echo '<td>', $city_b ,'</td>';
}
echo '</tr>';

foreach(array_keys($distMatrix) as $city_a) { // need the city_a as row index
    echo '<tr>';
    echo '<td>', $city_a, '</td>'; // city_a ad
    foreach(array_keys($distMatrix[$city_a]) as $city_b) { // need the city_b as column index
        echo '<td>', $distMatrix[$city_a][$city_b], '</td>'; // distance from the matrix;
    }
    echo '</tr>';
}
echo '</table>';

测试数据 - 来自@ashkufaraz的数据

// changed the city ids so we can easily see city_a and city_b
$tableRows[0]=array("id"=>1, "id_city_a"=>1, "id_city_b"=>11, "distance"=>0);
$tableRows[1]=array("id"=>2, "id_city_a"=>1, "id_city_b"=>12, "distance"=>8);
$tableRows[2]=array("id"=>3, "id_city_a"=>1, "id_city_b"=>13, "distance"=>6);
$tableRows[3]=array("id"=>4, "id_city_a"=>2, "id_city_b"=>11, "distance"=>8);
$tableRows[4]=array("id"=>5, "id_city_a"=>2, "id_city_b"=>12, "distance"=>0);
$tableRows[5]=array("id"=>6, "id_city_a"=>2, "id_city_b"=>13, "distance"=>9);
$tableRows[6]=array("id"=>7, "id_city_a"=>3, "id_city_b"=>11, "distance"=>6);
$tableRows[7]=array("id"=>8, "id_city_a"=>3, "id_city_b"=>12, "distance"=>9);
$tableRows[8]=array("id"=>9, "id_city_a"=>3, "id_city_b"=>13, "distance"=>0);

输出:

#   11  12  13
1   0   8   6
2   8   0   9
3   6   9   0