如何将数组从 Zend Dom 查询结果传递到表 [英] How to Pass Array from Zend Dom Query Results to table

查看:22
本文介绍了如何将数组从 Zend Dom 查询结果传递到表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在查询 YEAR、RATING 列的 URL,我想将这两个列插入到数据库中.例如.年份评级1992 9.22000 91999 8........

I am querying a URL for columns YEAR, RATING and I want to insert those two into a database. E.g. YEAR RATING 1992 9.2 2000 9 1999 8 . . . . . . . .

所以,我正在编写这样的脚本:

So, I am writing script like:

$url = 'BLA BLA ';
$data = file_get_contents($url);
$dom = new Zend_Dom_Query($data);
$year = $dom->query('.secondaryInfo');
$rating = $dom->query('.ratingColumn');

如何将它们都插入到数据库中???

How to insert both of them in a database???

for ( .... What do I have to write here?)

$parse = new Application_Model_Parse();
$parse->createImdb(array(
'rating' => What do I have to write here?
'year' => What do I have to write here?
));

任何帮助都会有用.

谢谢

推荐答案

http://framework.zend.com/manual/1.12/en/zend.dom.query.html

拥有文档后,您可以使用 query()queryXpath() 方法;每个方法将返回一个带有任何匹配节点的 Zend_Dom_Query_Result 对象.

Once you have a document, you can use either the query() or queryXpath() methods; each method will return a Zend_Dom_Query_Result object with any matching nodes.

进一步

如前所述,Zend_Dom_Query_Result 实现了 IteratorCountable,因此可以在 foreach() 循环以及 count() 函数.

As mentioned previously, Zend_Dom_Query_Result implements both Iterator and Countable, and as such can be used in a foreach() loop as well as with the count() function.

各种迭代器方法将返回PHP 原生 DOMElements 在这种情况下,这意味着您可以通过它的 nodeValue 属性访问元素的文本内容.

The various Iterator methods will return PHP's native DOMElements in this case, which means you can access the Element's text content via it's nodeValue property.

假设 Result 对象中只有一项,请尝试

Assuming there is only one item in the Result object, try

$parse->createImdb(
    array(
        'rating' => $rating->current()->nodeValue
        'year' => $year->current()->nodeValue
    )
);

如果您觉得使用迭代器不舒服,可以使用:

If you feel uncomfortable working with an Iterator, you can use:

  • iterator_to_array — Copy the iterator into an array

好吧,将迭代器复制到一个数组中,然后改用它,例如

to, well, copy the iterator into an array and then use that instead, e.g.

$rating = iterator_to_array($rating);
$year = iterator_to_array($year);
$parse->createImdb(
    array(
        'rating' => $rating[0]->nodeValue
        'year' => $year[0]->nodeValue
    )
);

如果有多个结果,用 MultipleIterator 将它们一起迭代

If there is multiple results, iterate them together with a MultipleIterator

$ratingsAndYears = new MultipleIterator();
$ratingsAndYears->attachIterator($rating);
$ratingsAndYears->attachIterator($year);

foreach ($ratingsAndYears as $ratingAndYear) {
    $parse = new Application_Model_Parse();
    $parse->createImdb(
        array(
            'rating' => $ratingAndYear[0]->nodeValue
            'year' => $ratingAndYear[1]->nodeValue
        )
    );

顺便说一句,Zend_Dom_Query 只是 PHP 原生 DOM 扩展的一个薄包装器,如果您知道 XPath,您实际上并不需要查询它提供的 W3 选择器,并且可以单独使用 DOM 完成所有操作.

On a sidenote, Zend_Dom_Query is just a thin wrapper around PHP's native DOM extension and if you know XPath, you don't really need the W3 Selector querying it offers and can do it all with DOM alone.

这篇关于如何将数组从 Zend Dom 查询结果传递到表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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