Hướng dẫn array to html table php - mảng sang bảng html php

Tôi có một nhu cầu tương tự, nhưng mảng của tôi có thể chứa key/value hoặc key/array hoặc array of arrays, như mảng này:

$array = array(
    "ni" => "00000000000000",
    "situacaoCadastral" => array(
        "codigo" => 2,
        "data" => "0000-00-00",
        "motivo" => "",
    ),
    "naturezaJuridica" => array(
        "codigo" => "0000",
        "descricao" => "Lorem ipsum dolor sit amet, consectetur",
    ),
    "dataAbertura" => "0000-00-00",
    "cnaePrincipal" => array(
        "codigo" => "0000000",
        "descricao" => "Lorem ips",
    ),
    "endereco" => array(
        "tipoLogradouro" => "Lor",
        "logradouro" => "Lorem i",
        "numero" => "0000",
        "complemento" => "",
        "cep" => "00000000",
        "bairro" => "Lorem ",
        "municipio" => array(
            "codigo" => "0000",
            "descricao" => "Lorem ip",
        ),
    ),
    "uf" => "MS",
    "pais" => array(
        "codigo" => "105",
        "descricao" => "BRASIL",
    ),
    "municipioJurisdicao" => array(
        "codigo" => "0000000",
        "descricao" => "DOURADOS",
    ),
    "telefones" => array(
        array(
            'ddd' => '67',
            'numero' => '00000000',
        ),
    ),
    "correioEletronico" => "",
    "capitalSocial" => 0,
    "porte" => "00",
    "situacaoEspecial" => "",
    "dataSituacaoEspecial" => ""
);

Chức năng tôi tạo để giải quyết là:

function array_to_table($arr, $first=true, $sub_arr=false){
    $width = ($sub_arr) ? 'width="100%"' : '' ;
    $table = ($first) ? '<table align="center" '.$width.' bgcolor="#FFFFFF" border="1px solid">' : '';
    $rows = array();
    foreach ($arr as $key => $value):
        $value_type = gettype($value);
        switch ($value_type) {
            case 'string':
                $val = (in_array($value, array(""))) ? "&nbsp;" : $value;
                $rows[] = "<tr><td><strong>{$key}</strong></td><td>{$val}</td></tr>";
                break;
            case 'integer':
                $val = (in_array($value, array(""))) ? "&nbsp;" : $value;
                $rows[] = "<tr><td><strong>{$key}</strong></td><td>{$value}</td></tr>";
                break;
            case 'array':
                if (gettype($key) == "integer"):
                    $rows[] = array_to_table($value, false);
                elseif(gettype($key) == "string"):
                    $rows[] = "<tr><td><strong>{$key}</strong></td><td>".
                        array_to_table($value, true, true) . "</td>";
                endif;
                break;
            default:
                # code...
                break;
        }
    endforeach;
    $ROWS = implode("\n", $rows);
    $table .= ($first) ? $ROWS . '</table>' : $ROWS;
    return $table;
}

echo array_to_table($array);

Và đầu ra là thế này

Tệp này chứa văn bản unicode hai chiều có thể được giải thích hoặc biên dịch khác với những gì xuất hiện dưới đây. Để xem xét, hãy mở tệp trong một trình soạn thảo cho thấy các ký tự Unicode ẩn. Tìm hiểu thêm về các ký tự unicode hai chiều

"; " . implode('', $cells) . "";
functionHtml_table ($ data = mảng ()) html_table($data = array())
{
$ rows = mảng ();rows = array();
foreach ($ dataas $ row) { ($data as $row) {
$ tế bào = mảng ();cells = array();
foreach ($ rowas $ cell) { ($row as $cell) {
$ tế bào [] = "{$ cell}";cells[] = "{$cell}
}
$ hàng [] = "". Implode ('', $ tế bào). "";rows[] = "
}
$ hàng [] = "". Implode ('', $ tế bào). ""; "" . implode('', $rows) . "
";
trở về "" . Implode ('', $ hàng). "";
}data = array(
$ data = mảng (('1' => '1', '2' => '2'),
mảng ('1' => '1', '2' => '2'),('1' => '111', '2' => '222'),
mảng ('1' => '111', '2' => '222'),('1' => '111111', '2' => '222222'),
mảng ('1' => '111111', '2' => '222222'),
); html_table($data);

Làm thế nào để tạo bảng bằng mảng trong PHP?

$ value) {$ html. = ''. php function build_table($array){ // start table $html = ''; // header row $html .= ''; foreach($array[0] as $key=>$value){ $html .= '
' .

Làm cách nào để chuyển dữ liệu từ bảng HTML sang mảng PHP?

PHP $ htmlContent = file_get_contents ("http://teskusman.esy.es/index.html");$ Dom = new DomDocument ();$ Dom-> loadHtml ($ htmlContent);$ Header = $ dom-> getElementsByTagName ('th');$ Chi tiết = $ dom-> getElementsByTagName ('td');//#Nhận tên tiêu đề của bảng foreach ($ tiêu đề là $ nodeheader) {$ adatatableHeaderHtml [] = trim ($ ...

Làm cách nào để tạo một bảng trong PHP bằng HTML?

Bạn có thể tạo một bảng bằng phần tử.Bên trong phần tử, bạn có thể sử dụng các phần tử để tạo hàng và để tạo các cột bên trong một hàng, bạn có thể sử dụng các phần tử.Bạn cũng có thể định nghĩa một ô là tiêu đề cho một nhóm các ô bảng sử dụng phần tử.using the element. Inside the
element, you can use the elements to create rows, and to create columns inside a row you can use the
elements. You can also define a cell as a header for a group of table cells using the element.

Làm cách nào để lặp lại một mảng trong PHP?

Làm cách nào để lặp lại một mảng trong PHP?Sử dụng Foreach Loop để lặp lại hoặc in một mảng trong PHP.Sử dụng hàm print_r () để lặp lại hoặc in một mảng trong PHP.Sử dụng hàm var_dump () để lặp lại hoặc in một mảng trong PHP.Use foreach Loop to Echo or Print an Array in PHP. Use print_r() Function to Echo or Print an Array in PHP. Use var_dump() Function to Echo or Print an Array in PHP.