0

所以我有这个功能

function parse_records($html) {
    foreach($html->find('li.vcard') as $vcard):
        $table = array();  
        foreach($vcard->find('span.given-name') as $given_name):                    
            $table['given_name'] = (trim(addslashes($given_name->plaintext), " "));
        endforeach; 
        foreach($vcard->find('span.family-name') as $family_name):
            $table['family_name'] = (trim(addslashes($family_name->plaintext)," "));
        endforeach;
        foreach($vcard->find('span.location') as $location):
            $table['location'] = (trim(addslashes($location->plaintext), " "));
        endforeach;
        foreach($vcard->find('span.industry') as $industry):
            $table['industry'] = (trim(addslashes($industry->plaintext), " "));
        endforeach;
        foreach($vcard->find('dd.current-content') as $headline):
            $table['headline'] = (trim(addslashes($headline->plaintext), " "));
        endforeach;
        foreach($vcard->find('a.btn-primary') as $url):
            $table['url'] = addslashes($url->href);
        endforeach;
        return $table;
    endforeach;

在我的主文件中,我像这样使用它。

$page = curl_request($fn, $ln);
$records = parse_records($page);
print_r($records);

但它仅适用于 1 条记录。我应该修改什么以便我可以传递正在传递的全部记录?我试过(trim(addslashes($given_name->plaintext), " "))了,但没有用。

4

2 回答 2

0

您需要像这样附加您的数组:

function parse_records($html) {

    // Array to hold all records
    $table = array();  

    foreach($html->find('li.vcard') as $vcard):

        // New array for each record
        $row = array();

        foreach($vcard->find('span.given-name') as $given_name):                    
            $row['given_name'] = (trim(addslashes($given_name->plaintext), " "));
        endforeach; 
        foreach($vcard->find('span.family-name') as $family_name):
            $row['family_name'] = (trim(addslashes($family_name->plaintext)," "));
        endforeach;
        foreach($vcard->find('span.location') as $location):
            $row['location'] = (trim(addslashes($location->plaintext), " "));
        endforeach;
        foreach($vcard->find('span.industry') as $industry):
            $row['industry'] = (trim(addslashes($industry->plaintext), " "));
        endforeach;
        foreach($vcard->find('dd.current-content') as $headline):
            $row['headline'] = (trim(addslashes($headline->plaintext), " "));
        endforeach;
        foreach($vcard->find('a.btn-primary') as $url):
            $row['url'] = addslashes($url->href);
        endforeach;

        // Add row to table
        $table[] = $row;

    endforeach;

    // wait till the end to return the whole thing
    return $table;
}

关键行是每个vcardforeachs 创建一个名为 $row 的数组,然后在该循​​环结束时,我们将整行添加到表中$table[] = $row;

于 2013-07-03T21:56:01.870 回答
0

它只返回一条记录的原因是你有这条线return $table;。这意味着您的代码执行foreach循环中的所有内容,然后当它遇到return语句时,它从函数返回该值- 即,停止从函数执行。

假设您想要从each $table生成的每个列表$vcard,您需要执行以下操作:

function parse_records($html)
{
    $tablesList = array();

    foreach($html->find("li.vcard") as $vcard)
    {
        $table = array();

        // Add keys to the table like you are doing.

        $tablesList[] = $table; // Add this table to the tables list.
    }

    // Return the list of all of the tables we produced.

    return $tablesList;
}

在这种情况下,我将$table您的循环生成的每个数组添加到一个名为 的数组$tablesList中,然后我将返回整个表列表 - 而不仅仅是生成的第一个$table

于 2013-07-03T21:57:14.677 回答