对模糊标题感到抱歉。我使用simple_html_dom从下面指定的设置中提取一些数据。我想要做的是将数据插入一个2D数组(?),其中第一个字段的值是订阅的名称,其余的是与该订阅相关的数据。使用foreach循环从数组创建2D阵列
<td><a href="/subname/index.jsp">SubName</a></td> <!-- This is the name of the subscription -->
<td>Comment regarding the subscription</td><!-- Comment -->
<td><strong>0,-</strong></td><!-- Monthly fee -->
<td>0,49</td><!-- Price per minute -->
<td>0,49</td><!-- Price per SMS -->
<td>1,99</td><!-- Price per MMS -->
我到目前为止,工作正常,但它将所有的值放入一个常规数组中。 我已经尝试阅读数组,并尝试不同的解决方案,但我只是似乎无法包裹我的头。
我想是这样的:
阵列 ( [SubName1] =>数组 ( [0] =>注释 [1] =>每月费用 [2] =>价格,每分钟 [3] =>单价SMS [4] =>单价MMS ) [SubName2] =>数组 (..)
这是我的代码:
function getData($uri) {
try {
$html = file_get_html($uri); // Fetch source code
$data = array();
foreach($html->find('td') as $td) { // Fetch all <td>-elements
foreach($td->find('a') as $a) { // Fetch all <a>-elements to remove links
$data[] = $a->innertext; // This returns the names of the subscriptions
}
foreach($td->find('strong') as $strong) { // Fetch all <strong>-elements to remove bold text
$data[] = $strong->innertext;
}
if(!preg_match('/<strong>/', $td->innertext) && !preg_match('/<a/', $td->innertext)) { // Skip all <td>-elements that contains <strong> and <a>, since we already have them
$data[] = $td->innertext;
}
}
/* Logic for database insertion goes here */
unset($data); // Deletes array
$html->clear(); // Clear to free up memory
unset($html);
} catch (Exception $e) {
echo 'Failed to fetch prices from'.$uri.'.<br />'.$e->getMessage();
}
}
在此先感谢。
是HTML文档XHTML吗?如果是这样,你可以使用PHP DOM扩展来解析代码并构建一个数组。请参阅:http://php.net/manual/en/book.dom.php – Camsoft 2010-02-03 11:31:01