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
)
4
  • You can use array_merge. Commented Oct 16, 2022 at 10:25
  • You can use the Arr::flatten method provided by the Laravel framework. Commented Oct 16, 2022 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 Commented Oct 16, 2022 at 10:52
  • Spread the exploded data while pushing: How to merge two arrays without adding another index Commented Oct 16, 2022 at 11:37

2 Answers 2

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();
Sign up to request clarification or add additional context in comments.

5 Comments

["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
what is the output of explode('\n', $node->text())[0]? try this: $rows->flatten()->toJson();
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();
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) { .... }
oh, I forgot that. I update the answer and use an arrow function for this issue. tnx @M.Eriksson
0

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

Does this help ?

1 Comment

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?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.