从函数返回一个数组

时间:2014-02-04 05:31:57

标签: php arrays function

我使用PHP Simple HTML DOM Parser从页面中抓取一些结果。 目前我遇到了函数问题,因为它没有返回数组“$ result”。

非常感谢任何帮助:)

数组的结果:

array(1) { [0]=> array(6) { ["itemid"]=> string(6) "123456" ["title"]=> string(21) "XXX Prod1" ["unit"]=> string(6) "500ml " ["price"]=> string(4) "2.59" } [1]=> array(6) { ["itemid"]=> string(6) "123457" ["title"]=> string(27) "XXX Prod2" ["unit"]=> string(6) "500ml " ["price"]=> string(5) "10.49" }

有问题的代码:

function parseItems($html) {
foreach($html->find('div.product-stamp-inner') as $content) { //Finds each individual product on page and extracts its details and stores it into its own array

    $detail['itemid'] = filter_var($content->find('a.product-title-link', 0)->href, FILTER_SANITIZE_NUMBER_FLOAT);
    $detail['title'] = $content->find('span.title', 0)->plaintext;
    $detail['unit'] = $content->find('span.unit-size', 0)->plaintext;
    $detail['price'] = filter_var($content->find('span.price', 0)->plaintext, FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION | FILTER_FLAG_ALLOW_THOUSAND);
    $result[] = $detail; //Puts all individual product arrays into one large array
    }
    //var_dump($result); --Testing purposes
    return $result;
}

1 个答案:

答案 0 :(得分:2)

我猜你有一段像这样的代码

parseItems($html);

当它应该是以下因为它返回一个变量并需要一个变量来保存它的返回结果

$retval = parseItems($html);