2010-02-03 86 views
0

对模糊标题感到抱歉。我使用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(); 
} 
} 

在此先感谢。

+0

是HTML文档XHTML吗?如果是这样,你可以使用PHP DOM扩展来解析代码并构建一个数组。请参阅:http://php.net/manual/en/book.dom.php – Camsoft 2010-02-03 11:31:01

回答

0

如果我正确理解你的问题,这是你应该怎么做的。

首先,我建议你捕捉每行而不是单个单元格,然后独立解析每一行。

因此,在这个例子中,我假设你行被包裹在tr标签:

<tr> 
<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 --> 
</tr> 

如果有更多的细胞在一开始还是你只是必须相应地调整指数结束。此外,我还没有测试过这个代码,所以可能有一些错误,但一般的想法应该没问题。

//here we will store parsed values 
$data = array(); 

// you may want to filter this a bit if you want some rows to be skipped 
foreach ($html->find('tr') as $tr) { 
    // we get first cell in the row, find a element inside and take it's inner text and so on 
    $name = $tr->children(1)->find('a')->innertext; 
    $comment = $tr->children(2)->innertext; 
    $monthyFee = $tr->children(3)->find('strong')->innertext; 
    $pricePerMin = $tr->children(4)->innertext; 
    $pricePerSms = $tr->children(5)->innertext; 
    $pricePerMms = $tr->children(6)->innertext; 

    // create new entry in $data array formatted as you wanted it 
    $data[$name] = array($comment, $monthlyFee, $pricePerMin, $pricePerSms, $pricePerMms); 
} 

重要提示在这里 - 这不会妨碍你的情况下,覆盖一些数据你的名字是不是唯一的,所以你必须确保它是否真的是。这是因为关联数组不能有多个具有相同值的键。

+0

谢谢,几个推文后,它得到了工作。无法使用children(x) - 发现剥离和强壮,但使用“明文”而不是innertext解决了问题。谢谢 :) – Tom 2010-02-03 12:08:24