将多列添加到数组

Adding multiple columns to array

我想将我通过网络抓取的几个值添加到最终结果数组中。我抓取的每个值代表数组中的一列。

看看下面我试过的东西:

<?php
require_once 'vendor/autoload.php';

use Goutte\Client;

$client = new Client();
$cssSelector = 'tr';
$coin = 'td.no-wrap.currency-name > a';
$url = 'td.no-wrap.currency-name > a';
$symbol = 'td.text-left.col-symbol';
$price = 'td:nth-child(5) > a';

$result = array();

$crawler = $client->request('GET', 'https://coinmarketcap.com/all/views/all/');

$crawler->filter($coin)->each(function ($node) {
    print $node->text()."\n";
    array_push($result, $node->text());
});

$crawler->filter($url)->each(function ($node) {
    $link = $node->link();
    $uri = $link->getUri();
    print $uri."\n";
    array_push($result, $uri);
});

$crawler->filter($symbol)->each(function ($node) {
    print $node->text()."\n";
    array_push($result, $node->text());
});

$crawler->filter($price)->each(function ($node) {
    print $node->text()."\n";
    array_push($result, $node->text());
});

print_r($result); 

我的问题是单个结果没有被推送到数组中。有什么建议吗?

是否有更好的方法将多个属性添加到数组?

感谢您的回复!

$result 在你的闭包中是未知的。

尝试使用 USE 将外部变量 $result 包含在过滤器闭包中,如下所示:

$crawler->filter($coin)->each(function ($node) use (&$result) {
    print $node->text()."\n";
    array_push($result, $node->text());
});

http://php.net/manual/en/class.closure.php