skip to Main Content

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
)

2

Answers


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

    Does this help ?

    Login or Signup to reply.
  2. 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();
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search