【问题标题】:Convert string into single array, PHP将字符串转换为单个数组,PHP
【发布时间】:2016-04-02 03:43:50
【问题描述】:

我在一个 Laravel 项目中工作。我想以有效的方式将字符串转换为单个数组。

字符串是 $string = txn_status=0|txn_msg=success|txn_err_msg=NA|clnt_txn_ref=969239|tpsl_bank_cd=470|tpsl_txn_id=192630337|txn_amt=1.00|clnt_rqst_meta={itc:NIC~TXN0001~122333~rt14154~8 mar 2014~Payment~forpayment}{custname:test}|tpsl_txn_time=26-12-2015 15:56:20|tpsl_rfnd_id=NA|bal_amt=NA|rqst_token=hdfs-df-jkfhskjfhsjkd|hash=jhdsfs54367jhf,

我希望输出看起来像下面的格式。是json格式:-

[ txn_status: "0",
  txn_msg : "success",
  txn_err_msg: "NA",
  .
  .
  .


  hash: "XYZ" ]

【问题讨论】:

    标签: php arrays string laravel


    【解决方案1】:

    您可以将preg_match_allarray_combine 结合使用,如下所示:

    $string = "txn_status=0|txn_msg=success|txn_err_msg=NA|clnt_txn_ref=969239|tpsl_bank_cd=470|tpsl_txn_id=192630337|txn_amt=1.00|clnt_rqst_meta={itc:NIC~TXN0001~122333~rt14154~8 mar 2014~Payment~forpayment}{custname:test}|tpsl_txn_time=26-12-2015 15:56:20|tpsl_rfnd_id=NA|bal_amt=NA|rqst_token=hdfs-df-jkfhskjfhsjkd|hash=jhdsfs54367jhf";
    
    preg_match_all("/([^|]+)=([^|]+)/", $string, $array);
    
    $output = array_combine($array[1], $array[2]);
    
    echo json_encode($output, JSON_PRETTY_PRINT);
    

    http://ideone.com/eXf5K2

    或者像这样的preg_split

    $array = preg_split("/[|=]/", $string);
    $output = [];
    
    for ($i=0; $i<count($array); $i++) {
        $output[$array[$i]] = $array[++$i];
    }
    

    http://ideone.com/Y5k5bV

    或者@devpro的代码的简化版:

    $array = explode("|", $string);
    $output = [];
    
    foreach ($array as $v) {
        list($key, $value) = explode("=", $v);
        $output[$key] = $value;
    }
    

    http://ideone.com/svrj8S

    【讨论】:

    • 嘿 pespantelis,感谢您回答多种解决方案。如果我想在上面的数组中打破 {key1:value1} {key2:value2}....{keyN:valueN},我可以知道解决方案吗?例如,整个值 - {itc:NIC~TXN0001~122333~rt14154~8 mar 2014~Payment~forpayment} {custname:test}
    【解决方案2】:

    你可以像这样拆分试试这个:

    $string = "txn_status=0|txn_msg=success|txn_err_msg=NA|clnt_txn_ref=969239|tpsl_bank_cd=470|tpsl_txn_id=192630337|txn_amt=1.00|clnt_rqst_meta={itc:NIC~TXN0001~122333~rt14154~8 mar 2014~Payment~forpayment}{custname:test}|tpsl_txn_time=26-12-2015 15:56:20|tpsl_rfnd_id=NA|bal_amt=NA|rqst_token=hdfs-df-jkfhskjfhsjkd|hash=jhdsfs54367jhf";
    $firstArray = explode("|", $string);
    foreach ($firstArray as $key => $value) {
        $newArr = explode("=", $value);
        $myRequiredArr[$newArr[0]] = $newArr[1];
    }
    
    echo "<pre>"; // just for formatting
    print_r($myRequiredArr); // print your result
    

    结果是:

    Array
    (
        [txn_status] => 0
        [txn_msg] => success
        [txn_err_msg] => NA
        [clnt_txn_ref] => 969239
        [tpsl_bank_cd] => 470
        [tpsl_txn_id] => 192630337
        [txn_amt] => 1.00
        [clnt_rqst_meta] => {itc:NIC~TXN0001~122333~rt14154~8 mar 2014~Payment~forpayment}{custname:test}
        [tpsl_txn_time] => 26-12-2015 15:56:20
        [tpsl_rfnd_id] => NA
        [bal_amt] => NA
        [rqst_token] => hdfs-df-jkfhskjfhsjkd
        [hash] => jhdsfs54367jhf
    )
    

    【讨论】:

    • 这不会给 OP 他们期望的结果。它将给出一个值列表,如“txn_status=0”,而 OP 希望 txn_status 作为键,0 作为值。
    • 抱歉,它给出的结果是这样的 ["txn_status=0", "txn_msg=success",....],但我不想要这种格式。
    【解决方案3】:

    你可以使用explodearray_mapcall_user_func_array这样的php函数组合

    $string = "txn_status=0|txn_msg=success|txn_err_msg=NA|clnt_txn_ref=969239|tpsl_bank_cd=470|tpsl_txn_id=192630337|txn_amt=1.00|clnt_rqst_meta={itc:NIC~TXN0001~122333~rt14154~8 mar 2014~Payment~forpayment}{custname:test}|tpsl_txn_time=26-12-2015 15:56:20|tpsl_rfnd_id=NA|bal_amt=NA|rqst_token=hdfs-df-jkfhskjfhsjkd|hash=jhdsfs54367jhf";
    $arr = array();
    array_map(function($v)use(&$arr){ $a = explode("=",$v); return $arr[$a[0]]  = $a[1];},explode('|',$string));
    print_r($arr);
    

    Working Demo

    【讨论】:

      猜你喜欢
      • 2015-11-24
      • 1970-01-01
      • 2018-01-29
      • 2013-08-12
      • 2021-12-27
      • 1970-01-01
      • 2010-10-15
      相关资源
      最近更新 更多