如何合并2个数组并在表中打印?

时间:2016-02-25 19:35:37

标签: php html

我想合并$output$output1,然后填充HTML表格。

这是我的代码:

    <?php
$link = parse_ini_file(__DIR__ . '/config.ini', true);
include("connect.php");



$output = '';
$cnn = simplexml_load_file($link['cnn']);
$bbc = simplexml_load_file($link['bbc']);

    foreach($cnn->channel->item as $item){



         $title = $item->title;
         $description = $item->description;
         $url = $item->link;
         $pubDate = $item->pubDate;
         $title1 = str_replace("'","\'", $title);
         $description1 = str_replace("'","\'", $description);



$output[]['title'] = $title;
$output[]['description'] = $description;
$output[]['url'] = $url;
$output[]['p_date'] = $pubDate; 



            $sql = mysql_query("INSERT IGNORE INTO tbl_daily_news_headlines (
                title,
                description,
                url,
                pub_date,
                log_date)
            VALUES (
                '$title1',
                '$description1',
                '$url',
                '$pubDate',
                now())")
            or die(mysql_error());

        }

                foreach ($bbc->channel->item as $bitem){

                 $bbtitle = $bitem->title;
                 $bbdescription = $bitem->description;
                 $bburl = $bitem->link;
                 $bbpubDate = $bitem->pubDate;
                 $bbtitle1 = str_replace("'", "\'", $bbtitle);
                 $bbdescription1 = str_replace("'", "\'", $bbdescription);  

$output1[]['title'] = $bbtitle;
$output1[]['description'] = $bbdescription;
$output1[]['url'] = $bburl;
$output1[]['p_date'] = $bbpubDate;    


    $sql = mysql_query("INSERT IGNORE INTO tbl_daily_news_headlines(
        title,
        description,
        url,
        pub_date,
        log_date)
    VALUES( 
        '$bbtitle1',
        '$bbdescription1',
        '$bburl',
        '$bbpubDate',now())")
    or die(mysql_error());

$final_output = array_merge($output, $output1);


            }


?>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="initial-scale=1.0, maximum-scale=2.0">
    <title>Feed Example</title>
    <link rel="stylesheet" type="text/css" href="media/css/bootstrap.min.css">
    <link rel="stylesheet" type="text/css" href="media/css/dataTables.bootstrap.css">


    <script type="text/javascript" language="javascript" src="media/js/jquery-1.12.0.min.js">
    </script>
    <script type="text/javascript" language="javascript" src="media/js/jquery.dataTables.js">
    </script>
    <script type="text/javascript" language="javascript" src="media/js/dataTables.bootstrap.js">
    </script>
    <script type="text/javascript" language="javascript" class="init">

$(document).ready(function() {
    $('#example').DataTable();
} );

    </script>
</head>
<body class="dt-example dt-example-bootstrap">
    <div class="container">
        <section>
            <h1>FEED Test</h1>
            <div class="info">
            </div>
            <table id="example" class="table table-striped table-bordered" cellspacing="0" width="100%">
                <thead>
                    <tr>
                        <th>Title</th>
                        <th>Description</th>
                        <th>URL</th>
                        <th>Publish Date</th>

                    </tr>
                </thead>
                <tfoot>
                    <tr>
                        <th>Title</th>
                        <th>Description</th>
                        <th>URL</th>
                        <th>Publish Date</th>
                    </tr>
                </tfoot>
                <tbody>
            <? foreach($final_output as $data1)
          {
            echo '<td>'.$data1['title'].$data1['description'].$data1['url'].$data1['p_date'].'</td>';

          }


        ?>
                </tbody>
            </table>

            </div>
    </section>
</body>
</html>

我试过了array_merge(),但它没有用。

我也尝试过:

$data = $output + $output1;

我错在哪里,我是否错误地放置了我的循环?

修改 所以在我尝试array_merge($ output,$ output1)之后;它只输出数组的最后一个元素,第二个方法是相同的

我只是尝试做一些内在的foreach,比如

            <? foreach($output as $data)
               {
                foreach($output1 as $data1)
                  {
                  echo '<td>'.$data.$data1'</td>';
                  }
                }
        ?>

但我有500错误,所以有任何想法吗?

编辑2: 感谢Patrick我合并这两个输出,但仍然不能打印在桌子上,如果你现在看我更新的代码它水平打印它们(一切都在1车道),如何修复?

enter image description here

2 个答案:

答案 0 :(得分:1)

您只获得一个元素的原因是您每次通过foreach循环覆盖之前的值。您需要每次在$output$output1数组中创建一个新条目。

而不是

    $output['title'] = $title;
    $output['description'] = $description;
    $output['url'] = $url;
    $output['p_date'] = $pubDate;

    $output1['title'] = $bbtitle;
    $output1['description'] = $bbdescription;
    $output1['url'] = $bburl;
    $output1['p_date'] = $bbpubDate;    

你应该

    $output[]['title'] = $title;
    $output[]['description'] = $description;
    $output[]['url'] = $url;
    $output[]['p_date'] = $pubDate; 

    $output1[]['title'] = $bbtitle;
    $output1[]['description'] = $bbdescription;
    $output1[]['url'] = $bburl;
    $output1[]['p_date'] = $bbpubDate;    

答案 1 :(得分:0)

对于DataTables,您希望它采用某种JSON格式。这是我如何使用你在那里做的,并快速(未经测试)

<?php

$final_output = array_merge($output, $output1);

和HTML / Javascript

<script type="text/javascript">
    var php_data = <?php echo json_encode($final_output); ?>

    $("#example").DataTable({
        data: php_data
    });

</script>

<table id="example">
    <thead>
        <tr>
            <th>Title</th>
            <th>Description</th>
            <th>URL</th>
            <th>Publish Date</th>
        <tr>
    </thead>
</table>

然而,更好的方法是通过ajax。查看DataTables的文档:https://www.datatables.net/examples/ajax/

以及如果不使用ajax,如何格式化数据的示例:

https://www.datatables.net/examples/data_sources/js_array.html