在函数中推送数组

时间:2013-07-03 21:52:04

标签: php

所以我有这个功能

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), " ")),但无济于事。

2 个答案:

答案 0 :(得分: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;
}

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

的表中

答案 1 :(得分:0)

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

假设您需要从每个$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的数组中,然后我将返回整个表列表 - 而不仅仅是第一个{{ 1}}生成。