PHP关联数组 key value_PHP:如何将key=value文件快速拆分为关联数组

当你发送一个PDT交易ID回贝宝,你会得到一个交易数据列表。它在第一行成功,然后是key=value对列表。每行一对。例如:

SUCCESS

business=paypal@example.com

charset=windows-1252

custom=

first_name=Alice

handling_amount=0.00

invoice=NN0005

item_name=Bear

item_number=BEAR05

last_name=Foobar

mc_currency=SEK

mc_fee=13.00

mc_gross=250.00

payer_email=bob@example.com

payer_id=UC9DXVX7GRSTN

payer_status=unverified

payment_date=09:08:06 Oct 18, 2010 PDT

payment_fee=

payment_gross=

payment_status=Completed

payment_type=instant

protection_eligibility=Ineligible

quantity=1

receipt_id=2479-2605-1192-2880

receiver_email=paypal@example.com

receiver_id=8Y670ENTB8BY6

residence_country=NO

shipping=0.00

tax=0.00

transaction_subject=Bear

txn_id=1PH815997L239283J

txn_type=web_accept

检查第一行是否成功,然后将其转换为关联数组的好的、快速的和干净的方法是什么?我能做到,而且很有效,但我很好奇是否有更好或更干净的方法来做,因为我最终得到的并不总是那么好。注意,有些键也没有任何值。

array(

'business' => 'paypal@example.com',

'charset' => 'windows-1252',

'custom' => NULL,

'first_name' => Alice,

// And so on

);

顺序无关紧要。

谢谢你的建议!现在测试他们。顺便说一下,把字符串分成几行也是我的问题之一。忘了具体说明。注意,有些方法已经考虑到了这一点,有些则没有考虑到,这可能会产生影响,因为某些方法需要先分成几行,然后再分成两对,而另一些方法则可以一块吃掉整件东西。

我还应该提到,空的结果是空的,这是一种奖励,但可能不是一个要求。他们在我的版本中也没有这样做,也没什么大不了的。

基准结果

我很好奇我应该在这里选择什么,所以我基准我应该如何做不同的部分。我自己有各种各样的想法,从这里和其他地方也得到了一些。当我找到了我能做到的最快速度时,我创建了一个基准,并将它与迄今为止的所有答案进行了对比。对于那些跳过拆分或检查成功的用户,我添加了一个

explode

以及

strpos

相应检查。我还将urldecode添加到所有解决方案中,除了@dynamism处理得非常好。不管怎样,结果如下:

2MB2W.jpg

/**

* Test various methods of checking for SUCCESS

*

* @package PayPal

* @category PDT

* @author Torleif Berger

*/

class Bench_ProcessPDT extends Codebench

{

public $description = 'Various ways of checking that a string starts with SUCCESS';

public $loops = 100000;

public function bench_mine($subject)

{

if(strpos($subject, 'SUCCESS') === 0)

{

$subject = urldecode(substr($subject, 7));

preg_match_all('/^([^=]++)=(.*+)/m', $subject, $result, PREG_PATTERN_ORDER);

$result = array_combine($result[1], $result[2]);

return array(count($result), array_shift($result), array_shift($result));

}

return FALSE;

}

// http://stackoverflow.com/questions/3964219/3964308#3964308

public function bench_dynamism_substr($subject)

{

if(substr($subject, 0, 7) == 'SUCCESS')

{

$subject = substr_replace($subject, '', 0, 7);

$subject = str_replace(array("\n", "\r", "\r\n"), '&', $subject);

parse_str($subject, $result);

return array(count($result), array_shift($result), array_shift($result));

}

return FALSE;

}

// http://stackoverflow.com/questions/3964219/3964308#3964308

public function bench_dynamism_strpos($subject)

{

if(strpos($subject, 'SUCCESS') === 0)

{

$subject = substr_replace($subject, '', 0, 7);

$subject = str_replace("\r\n", '&', $subject);

parse_str($subject, $result);

return array(count($result), array_shift($result), array_shift($result));

}

return FALSE;

}

// http://stackoverflow.com/questions/3964219/3964520#3964520

public function bench_mellowsoon($subject)

{

$subject = urldecode($subject);

$lines = explode("\r\n", $subject);

$lines = array_map('trim', $lines);

$status = array_shift($lines);

if($status == 'SUCCESS')

{

$result = array();

foreach($lines as $line)

{

list($key, $value) = explode('=', $line, 2);

$result[$key] = $value;

}

return array(count($result), array_shift($result), array_shift($result));

}

return FALSE;

}

// http://stackoverflow.com/questions/3964219/3964265#3964265

public function bench_amber($subject)

{

if(strpos($subject, 'SUCCESS') === 0)

{

$subject = explode("\r\n", urldecode($subject));

array_shift($subject); // Remove is empty

$result = array();

foreach($subject as $line)

{

$bits = explode('=', $line);

$field_name = array_shift($bits);

$field_contents = implode('=', $bits);

$result[$field_name] = $field_contents;

}

return array(count($result), array_shift($result), array_shift($result));

}

return FALSE;

}

// http://stackoverflow.com/questions/3964219/3964366#3964366

public function bench_GigaWatt($subject)

{

if(strpos($subject, 'SUCCESS') === 0)

{

$subject = explode("\r\n", urldecode($subject));

$result = array();

foreach($subject as $line)

{

if (strpos($line, "=") === FALSE)

continue;

list($var, $value) = explode("=", trim($line));

$result[$var] = empty($value) ? NULL : $value;

}

return array(count($result), array_shift($result), array_shift($result));

}

return FALSE;

}

// http://stackoverflow.com/questions/3964219/3964366#3964366

public function bench_GigaWatt2($subject)

{

if(strpos($subject, 'SUCCESS') === 0)

{

$subject = explode("\r\n", urldecode($subject));

$result = array();

foreach($subject as $line)

{

if (strpos($line, "=") === FALSE)

continue;

list($var, $value) = explode("=", trim($line));

$result[$var] = $value;

}

return array(count($result), array_shift($result), array_shift($result));

}

return FALSE;

}

// http://stackoverflow.com/questions/3964219/3964323#3964323

public function bench_dvhh($subject)

{

if(strpos($subject, 'SUCCESS') === 0)

{

$subject = explode("\r\n", urldecode($subject));

$result = array();

foreach($subject as $line)

{

$lineData = preg_split("/\s*=\s*/", $line);

if(count($lineData) == 2)

{

$result[$lineData[0]] = $lineData[1];

}

}

return array(count($result), array_shift($result), array_shift($result));

}

return FALSE;

}

public $subjects = array

(

"SUCCESS\r\nbusiness=paypal@business.example.com\r\ncharset=windows-1252\r\ncustom=\r\nfirst_name=Alice\r\nhandling_amount=0.00\r\ninvoice=AF000001\r\nitem_name=Stuffed bear\r\nitem_number=BEAR05\r\nlast_name=Foobar\r\nmc_currency=USD\r\nmc_fee=2.00\r\nmc_gross=20.00\r\npayer_email=alice.foobar@example.com\r\npayer_id=UC9DXVX7GRSTN\r\npayer_status=unverified\r\npayment_date=09:08:06 Oct 18, 2010 PDT\r\npayment_fee=\r\npayment_gross=\r\npayment_status=Completed\r\npayment_type=instant\r\nprotection_eligibility=Ineligible\r\nquantity=1\r\nreceipt_id=2479-2605-1192-2880\r\nreceiver_email=paypal@example.com\r\nreceiver_id=8Y670ENTB8BY6\r\nresidence_country=USD\r\nshipping=0.00\r\ntax=0.00\r\ntransaction_subject=Bear\r\ntxn_id=1PH815997L239283J\r\ntxn_type=web_accept",

"FAIL\r\nError: 4003",

"INVALID",

);

}

如果有人有任何进一步的改进建议,请告诉我:)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值