php - 将字符串(数组格式)转换为数组 - PHP

标签 php codeigniter

我正在使用来自 post 的数据通过 CURL 发送一串数据,并接收一个字符串形式的响应,该响应以数组的形式出现。我正在尝试将此类响应转换为实际数组。这是我到目前为止的代码:

我得到的响应字符串是这样的:

Test transaction_id="2401_02-29-12_22:28:34_0" action="payment" result="success" to_from="to" amount="205.00" gross="205.00" net="-0.85" custom="" business="XXXX@XXXX.com" item_name="XXXXX.XXX" item_code="Product Name 3," quantity="1" transaction_type="authorized payment" transaction_status="pending" transaction_date="2012-02-29 22:28:34" processing_rate="0.85" discount_fee="0.00" first_name="TESTNAME" last_name="TESTLNAME" address="TESTADDRESS" city="TESTCITY" state_or_province="AL" country="US" zip_or_postal_code="12345" phone="1234562002" email="email@wemail.es" shipment="yes" shipping_address="TESTADDRESS" shipping_city="TESTCITY" shipping_state_or_province="AL" shipping_country="US" shipping_zip_or_postal_code="12345" processing_time="0.2187"

我用来获得这种响应的代码是

<?php

// Get variables from POST array into a string
$post_str = "action=payment&business="      .urlencode($this->input->post('business'))
    ."&vericode="                   .urlencode($this->input->post('vericode'))
    ."&item_name="                  .urlencode($this->input->post('item_name'))
    ."&item_code="                  .urlencode($this->input->post('item_code'))
    ."&quantity="                   .urlencode($this->input->post('quantity'))
    ."&amount="                     .urlencode($this->input->post('amount'))
    ."&cc_type="                    .urlencode($this->input->post('cc_type'))
    ."&cc_number="                  .urlencode($this->input->post('cc_number'))
    ."&cc_expdate="                 .urlencode($this->input->post('cc_expdate_year')).urlencode($this->input->post('cc_expdate_month'))
    ."&cc_security_code="           .urlencode($this->input->post('cc_security_code'))
    ."&shipment="                   .urlencode($this->input->post('shipment'))
    ."&first_name="                 .urlencode($this->input->post('first_name'))
    ."&last_name="                  .urlencode($this->input->post('last_name'))
    ."&address="                    .urlencode($this->input->post('address'))
    ."&city="                       .urlencode($this->input->post('city'))
    ."&state_or_province="          .urlencode($this->input->post('state_or_province'))
    ."&zip_or_postal_code="         .urlencode($this->input->post('zip_or_postal_code'))
    ."&country="                    .urlencode($this->input->post('country'))
    ."&shipping_address="           .urlencode($this->input->post('shipping_address'))
    ."&shipping_city="              .urlencode($this->input->post('shipping_city'))
    ."&shipping_state_or_province=" .urlencode($this->input->post('shipping_state_or_province'))
    ."&shipping_zip_or_postal_code=".urlencode($this->input->post('shipping_zip_or_postal_code'))
    ."&shipping_country="           .urlencode($this->input->post('shipping_country'))
    ."&phone="                      .urlencode($this->input->post('phone'))
    ."&email="                      .urlencode($this->input->post('email'))
    ."&ip_address="                 .urlencode($this->input->post('ip_address'))
    ."&website_unique_id="          .urlencode($this->input->post('website_unique_id'));

// Send URL string via CURL
$backendUrl = "https://www.veripayment.com/integration/index.php";
$this->curl->create($backendUrl);
$this->curl->post($post_str);

// get response from API
$return_str = $this->curl->execute();
?>

最佳答案

如果您收到的响应包含由您可能使用的空格分隔的元素

function parse($response)
{
    $result = array();
    $resparray = explode(' ', $response);
    if ($resparray)
    {
      foreach ($resparray as $resp) {
        $keyvalue = explode('=', $resp);
        $result[$keyvalue[0]] =  str_replace('"', '', $keyvalue[1]);
      }
    }
    return $result;
}

编辑和更正,这是输出

Array ( [transaction_id] => 2401_02-29-12_22:28:34_0 [action] => payment [result] => success [to_from] => to [amount] => 205.00 [gross] => 205.00 [net] => -0.85 [custom] => [business] => XXXX@XXXX.com [item_name] => XXXXX.XXX [item_code] => Product [Name] => [3,"] => [quantity] => 1 [transaction_type] => authorized [payment"] => [transaction_status] => pending [transaction_date] => 2012-02-29 [22:28:34"] => [processing_rate] => 0.85 [discount_fee] => 0.00 [first_name] => TESTNAME [last_name] => TESTLNAME [address] => TESTADDRESS [city] => TESTCITY [state_or_province] => AL [country] => US [zip_or_postal_code] => 12345 [phone] => 1234562002 [email] => email@wemail.es [shipment] => yes [shipping_address] => TESTADDRESS [shipping_city] => TESTCITY [shipping_state_or_province] => AL [shipping_country] => US [shipping_zip_or_postal_code] => 12345 [processing_time] => 0.2187 

关于php - 将字符串(数组格式)转换为数组 - PHP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9521274/

相关文章:

javascript - PHP post提交未提交

php - 无法在嵌套 foreach 循环中多次循环遍历 mysql 结果

php - Codeigniter 3 - 从一个表中计算行数并加入

php - 我的脚本中是否存在内存泄漏,或者是另一个罪魁祸首?

php - CodeIgniter GROUP_CONCAT 并加入

php - 在 PHP 页面之间传递信息

php - 在限制关系存在的同时查询 Laravel 中的多对多关系

php - 选择具有多个相同项的位置

php - 子查询返回多行

php - 如何使用 CodeIgniter 设置 header() 并回显图像?