简体   繁体   English

PHP curl数组处理

[英]php curl array handling

First of all Hello I am currenttly scraping from a website the current price value of EURO & Dollars of my country to use them later for counting the total price value of the product the company bought., 首先, 您好,我目前正在从一个网站上抓取我所在国家的欧元和美元的当前价格值,以供以后用来计算公司购买的产品的总价格值。

[prix] => 
Array ( 
[0] => 212,00 DA    //Sell price
[1] => 213.00 DA    // Buy price
[2] => 184,00 DA    //Sell price
[3] => 185,00 DA  ) //Buy price
[nom] => 
Array ( 
[0] => Euro (€)  name of [0 and 1] prix array
[1] => Dollar US ($)) name of [ 2 and 3] prix array 

My Actual problem is that i want to parse the information of both array in once in a table (as shown below) : 我的实际问题是我想在表中一次解析两个数组的信息(如下所示):

CURRENCY NAME | 货币名称| SELL | 卖| Buy | 购买|

EURO | 欧元| 212,00 DA | 212,00 DA | 213,00 DA 213,00 DA

DOLLAR | 美元| 184,00 DA | 184,00 DA | 185,00 DA 185,00 DA

<?php
    $url='http://myurl/';
    $curl = curl_init();
    curl_setopt($curl,CURLOPT_URL, $url);
    curl_setopt($curl,CURLOPT_RETURNTRANSFER,true);
    $html = curl_exec($curl);
    ;
    $pro=array();
    preg_match_all("/<td>[0-9]{1,3}(\.[0-9]{3})*.[0-9]+ DA<\/td>/",$html,$match);
        $pro['prix']=$match['0'];

         preg_match_all('!<td><a href=".*">\K(.+?(?=<\/a><\/td>))!',$html,$match);
        $pro['nom']=$match['0'];

        curl_close($curl);

You can split the values into chunks and combine those with the keys. 您可以将值拆分为多个块,然后将其与键组合。

$currencies = array_combine($pro['nom'], array_chunk($pro['prix'], 2));

Then iterate the result of that to build your table. 然后迭代该结果以构建表。

foreach ($currencies as $currency => list($sell, $buy)) {
    var_dump($currency, $sell, $buy);
    // add whatever HTML you want
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM