Php 函数中的数组

Php 函数中的数组,php,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(a

所以我有这个函数

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);

但这只是一个记录。我应该修改什么,以便能够传递正在传递的所有记录?。我尝试了
(trim(addslashes($given_name->plaintext),“”)
,但没有效果。

您需要像这样附加数组:

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;
}
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;
}

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

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

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

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;
}
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
数组添加到名为
$tablelist
的数组中,然后返回整个表列表,而不仅仅是生成的第一个
$table

我认为您需要另一个
foreach
;)我试过你的代码,先生,但是当我打印r($records)时,它仍然会给我一条记录。@AjVillalobos那么你的代码还有一个问题,与数组附加无关。也许您的
$html->find('li.vcard')
没有返回您期望的结果-这是我开始重构之前的原始代码。它得到了页面的所有li.vcard。先生,它成功了。我拼错了变量名!非常感谢。是的,谢谢先生。现在我的问题是插入它们。我需要一个环吗
While($i;$iI)也尝试了你的,但像下面的sir一样,结果是一样的。而且你的代码似乎是一样的,我做错了什么?我想问1)你确定你的
return
语句不在
foreach
循环中吗?我推荐
{
}
,因为它们比
endforeach更容易阅读(IMO)方法,以及2)什么是
打印($html->find(“li.vcard”)
打印输出?