Adding Multiple Columns To Array - PHP
Maybe your like
I would like to add several values that I webscrape to a final result array. Each value that I scrape represents a column in the array.
See below what I tried:
<?php require_once 'vendor/autoload.php'; use GoutteClient; $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);My problem is that the individual results do not get pushed to the array. Any suggestions why?
Is there a better method to add multiple attributes to an array?
I appreciate your replies!
Advertisement
Answer
$result is not known in your closure.
try USE to include the external variable $result within the filter-closure, like so :
$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
Post navigation
Creating WooCommerce product variation adds an empty attribute value PHP Notice: Use of undefined constant php – assumed ‘php’Tags
.htaccess 110 Questions ajax 308 Questions apache 152 Questions api 126 Questions arrays 731 Questions codeigniter 273 Questions css 184 Questions curl 135 Questions database 169 Questions eloquent 271 Questions forms 179 Questions html 856 Questions javascript 837 Questions jquery 444 Questions json 276 Questions laravel 2243 Questions laravel-5 238 Questions laravel-8 180 Questions multidimensional-array 117 Questions mysql 1132 Questions mysqli 138 Questions pdo 134 Questions php 11445 Questions regex 239 Questions session 121 Questions sql 317 Questions string 109 Questions symfony 393 Questions woocommerce 599 Questions wordpress 1158 Questions
Tag » Add Column To Array Php
-
Adding Columns To Existing Php Arrays - Stack Overflow
-
Adding Column To Array Php Code Example - Code Grepper
-
How To Add Columns To An Existing Array - PHP
-
How To Add A Column To An Existing Multidimensional Php Array And ...
-
Array_column - Manual - PHP
-
PHP Array_column() Function - W3Schools
-
PHP Add Column To Two Dimentional Array
-
PHP Array Push: How To Add Elements To Array - AppDividend
-
Php. Adding New Column To An Array, But Is Doubling Up The Results
-
Adding Column To Array Php - Learn With IAP - IAcademyPlus
-
Add New Column/element To Collection - Laracasts
-
PHP Array_Column: Learn To Create Another Array From Array Column
-
Code For A Tutorial On Adding Columns To WordPress List Tables.
-
PHP Doctrine\DBAL\Schema Table::addColumn Examples