0

I try to scrape a currency rate from www.bi.go.id, my code like this

$client     = new Client();
$crawler    = $client->request('GET', 'https://www.bi.go.id/id/statistik/informasi-kurs/transaksi-bi/default.aspx');
$_data      = $crawler->filter('table[class="table table-striped table-no-bordered table-lg"] > tbody > tr')->each(function ($node) {
   $explode = explode('\n', $node->text());
   print_r($explode);
});

my problem it return a more than one array, how can i make this result to just one array and convert it to json

Array
(
    [0] => AUD 1 9.692,41 9.588,34
)
Array
(
    [0] => BND 1 10.753,75 10.646,01
)
Array
(
    [0] => CAD 1 11.173,37 11.058,20
)
Array
(
    [0] => CHF 1 15.444,59 15.281,75
)
Array
(
    [0] => CNH 1 2.145,88 2.124,29
)
Array
(
    [0] => CNY 1 2.146,68 2.124,79
)
  • You can use [array_merge](https://www.php.net/manual/en/function.array-merge.php). – Peppermintology Oct 16 '22 at 10:25
  • You can use the ``Arr::flatten`` method provided by the Laravel framework. – OMi Shah Oct 16 '22 at 10:33
  • `$_data = $crawler->filter('table[class="table table-striped table-no-bordered table-lg"] > tbody > tr')->each(function ($node) { $explode = explode('\n', $node->text()); print_r(Arr::flatten($explode)); }); ` i use this but still get same return – Ezha Syafaat Oct 16 '22 at 10:52
  • Spread the exploded data while pushing: [How to merge two arrays without adding another index](https://stackoverflow.com/a/73815010/2943403) – mickmackusa Oct 16 '22 at 11:37

2 Answers2

0

i think you can use $merged = array_merge(...$result); and then json_encode($merged); the new array

Does this help ?

coderpolo
  • 307
  • 3
  • 12
  • If your advised solution is to flatten the data with `array_merge(...$result);`, please do not post an answer, but flag the question as a duplicate of [How to Flatten a Multidimensional Array?](https://stackoverflow.com/q/1319903/2943403) – mickmackusa Oct 16 '22 at 11:28
0

make a collection then append items with push, at last for converting to JSON use toJson:

$rows = collect();
$_data = $crawler->filter('table[class="table table-striped table-no-bordered table-lg"] > tbody > tr')
    ->each(fn ($node) => $rows->push(explode('\n', $node->text())[0]));

$rows->toJson();
Nothehi
  • 695
  • 6
  • 19
  • `["AUD 1 9.692,41 9.588,34"]["BND 1 10.753,75 10.646,01"]["CAD 1 11.173,37 11.058,20"]["CHF 1 15.444,59 15.281,75"]["CNH 1 2.145,88 2.124,29"]["CNY 1 2.146,68 2.124,79"]` i try that but i get result like this – Ezha Syafaat Oct 16 '22 at 10:57
  • what is the output of `explode('\n', $node->text())[0]`? try this: `$rows->flatten()->toJson();` – Nothehi Oct 16 '22 at 11:05
  • Output `explode('\n', $node->text())[0]` is `AUD 1 9.692,41 9.588,34BND 1 10.753,75 10.646,01CAD 1 11.173,37 11.058,20CHF 1 15.444,59 15.281,75CNH 1 2.145,88 2.124,29` Output `$rows->flatten()->toJson();` is same like `$rows->toJson()`; – Ezha Syafaat Oct 16 '22 at 11:14
  • 1
    This should throw `undefined variable: $rows` inside the function and then a fatal with `Call to a member function push() on nulll`. The variable `$rows` are defined in the outside scope, and you never pass it into the function. If you want to use it in the function, you need `use`. Example: `function ($node) use ($rows) { .... }` – M. Eriksson Oct 16 '22 at 11:34
  • 1
    oh, I forgot that. I update the answer and use an arrow function for this issue. tnx @M.Eriksson – Nothehi Oct 16 '22 at 11:43