投入表从复杂的使用PHP数组 [英] Putting into table from a complicated array using php

查看:109
本文介绍了投入表从复杂的使用PHP数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

SimpleXMLElement Object
(
    [@attributes] => Array
        (
            [version] => 2.0
        )

    [channel] => SimpleXMLElement Object
        (
            [title] => Yahoo!ニュース・トピックス - トップ
            [link] => http://news.yahoo.co.jp/
            [description] => aa
            [language] => ja
            [pubDate] => Mon, 17 Aug 2015 10:20:57 +0900
            [item] => Array
                (
                    [0] => SimpleXMLElement Object
                        (
                            [title] => aa
                            [link] => aa
                            [pubDate] => aa
                            [enclosure] => SimpleXMLElement Object
                                (
                                    [@attributes] => Array
                                        (
                                            [length] => 133
                                            [url] => http://i.yimg.jp/images/icon/photo.gif
                                            [type] => image/gif
                                        )

                                    [0] => 

                                )

                            [guid] => yahoo/news/topics/6170952
                        )

                    [1] => SimpleXMLElement Object
                        (
                            [title] => bb
                            [link] => bb
                            [pubDate] => bb
                            [enclosure] => SimpleXMLElement Object
                                (
                                    [@attributes] => Array
                                        (
                                            [length] => 133
                                            [url] => http://i.yimg.jp/images/icon/photo.gif
                                            [type] => image/gif
                                        )

                                    [0] => 

                                )

                            [guid] => yahoo/news/topics/6170951
                        )

我有这个数组这是非常混乱,我作为一个初学者。
我只想把标题,链接和pubdate的 0 => SimpleXMLElement对象 1 => SimpleXMLElement对象 AA BB 在他们入我的表从MySQL拉。

I've got this array which is very confusing for me as a beginner. I just want to put the title, link and pubDate from 0=> SimpleXMLELement Object and 1 => SimpleXMLElement Object which has aa and bb in them into a my table pulled from mysql.

我的表是这样:

 Title
 PubDate
 Link

和我想要把AA和BB下的每个标题的表格中。

and I want to put the 'aa' and 'bb' under each of the titles in the table.

这是我曾尝试:

    foreach($con as $key => $val){
    while($key == title){
        $Title['title'] = $val;
    }
    return $Title['title'];
}

我试图做的是标记项和值 $键 $ VAL ,当 $键=标题,我希望把所有的冠军成一个阵列,但遗憾的是它没有工作。

What I was trying to do was label the keys and values with $key and $val, and when $key = title, I wanted to put all the titles into one array, but unfortunately it did not work.

推荐答案

假设$的XMLStructure是上面贴的结构,你的code应该是这个样子:

Assuming that $xmlStructure is the structure posted above, your code should look something like:

$items = $xmlStructure->channel->item;
$table = [];
foreach ($items as $item) {
    $table[] = [
        'title' => $item->title,
        'pubDate' => $item->pubDate,
        'link' => $item->link;
    ];
}

在本月底,你的$表数组包含实体的阵列,每个包含三个字段。

At the end of this, your $table array will contain an array of entities, each containing the three fields.

然后,构建一个HTML表(或其他类型的表)应该是相当简单的。

Then, constructing an HTML table (or table of another type) should be fairly straightforward.

这篇关于投入表从复杂的使用PHP数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆