将字符串转换为单个数组,PHP

时间:2015-12-28 09:38:49

标签: php arrays string laravel

我在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" ]

3 个答案:

答案 0 :(得分:5)

你可以像试试这样分开:

$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
)

答案 1 :(得分:2)

您可以将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

答案 2 :(得分:1)

你可以使用php函数explodearray_mapcall_user_func_array的组合,如

$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