1

我在表列中有以下值是否有任何选项可以获取sandbox值和merchant_email类似数组?我知道使用爆炸和循环是可行的,但还有其他智能选项吗?就像是$result->sandbox

 paypal_merchant_email="sample@ymail.com"|paypal_verified_only="0"|payment_currency=""|sandbox="1"|sandbox_merchant_email="sample@ymail.com"|payment_logos=""|debug="0"|status_pending="W"|status_success="O"|status_canceled="D"|countries=""|min_amount="0"|max_amount="0"|secure_post=""|ipn_test=""|no_shipping="0"|address_override="0"|cost_per_transaction="0"|cost_percent_total="0"|tax_id=0|

提前致谢!

4

6 回答 6

1
$string = 'paypal_merchant_email="sample@ymail.com"|paypal_verified_only="0"|payment_currency=""|sandbox="1"|sandbox_merchant_email="sample@ymail.com"|payment_logos=""|debug="0"|status_pending="W"|status_success="O"|status_canceled="D"|countries=""|min_amount="0"|max_amount="0"|secure_post=""|ipn_test=""|no_shipping="0"|address_override="0"|cost_per_transaction="0"|cost_percent_total="0"|tax_id=0|';

$result = array();
$string = preg_split('/\|/', $string);
foreach($string as $key => $value) 
{
    $value = str_replace('"', '', $value);
    $value = preg_split('/=/', $value);
    if(strlen($value[0])> 0) 
    {
       $result[$value[0]] = array_key_exists(1, $value) ? $value[1] : NULL;
    }
}

echo "<pre>";
print_r($result);
echo "</pre>";

输出

Array
(
    [paypal_merchant_email] => sample@ymail.com
    [paypal_verified_only] => 0
    [payment_currency] => 
    [sandbox] => 1
    [sandbox_merchant_email] => sample@ymail.com
    [payment_logos] => 
    [debug] => 0
    [status_pending] => W
    [status_success] => O
    [status_canceled] => D
    [countries] => 
    [min_amount] => 0
    [max_amount] => 0
    [secure_post] => 
    [ipn_test] => 
    [no_shipping] => 0
    [address_override] => 0
    [cost_per_transaction] => 0
    [cost_percent_total] => 0
    [tax_id] => 0
    [] => 
)
于 2013-09-24T03:36:56.337 回答
1

小基准魔术:

<?php
// explode
$start = microtime(TRUE);
$data = array();
foreach (explode('|', $result->sandbox) as $item)
{
    if (empty($item)) continue;
    list($key, $value) = explode("=", $item);
    $data[$key] = str_replace('"', '', $value);
}
print_r($data);
$stop = microtime(TRUE);
$timeResult = $stop - $start;
echo $timeResult, "\n";

//preg_split
$start = microtime(TRUE);
$data = array();
foreach (preg_split('/\|/', $result->sandbox) as $item)
{
    if (empty($item)) continue;
    list($key, $value) = preg_split('/=/', $item);
    $data[$key] = str_replace('"', '', $value);
}
print_r($data);
$stop = microtime(TRUE);
$timeResult = $stop - $start;
echo $timeResult, "\n";

//preg_match_all
$start = microtime(TRUE);
$data = array();
preg_match_all('/([^=]+)="([^"]+)?"\|/', $result->sandbox, $result);
$data = array_combine($result[1], $result[2]);
print_r($data);
$stop = microtime(TRUE);
$timeResult = $stop - $start;
echo $timeResult, "\n";

结果:

Array
(
    [paypal_merchant_email] => sample@ymail.com
    [paypal_verified_only] => 0
    [payment_currency] =>
    [sandbox] => 1
    [sandbox_merchant_email] => sample@ymail.com
    [payment_logos] =>
    [debug] => 0
    [status_pending] => W
    [status_success] => O
    [status_canceled] => D
    [countries] =>
    [min_amount] => 0
    [max_amount] => 0
    [secure_post] =>
    [ipn_test] =>
    [no_shipping] => 0
    [address_override] => 0
    [cost_per_transaction] => 0
    [cost_percent_total] => 0
    [tax_id] => 0
)
0.00029397010803223

Array
(
    [paypal_merchant_email] => sample@ymail.com
    [paypal_verified_only] => 0
    [payment_currency] =>
    [sandbox] => 1
    [sandbox_merchant_email] => sample@ymail.com
    [payment_logos] =>
    [debug] => 0
    [status_pending] => W
    [status_success] => O
    [status_canceled] => D
    [countries] =>
    [min_amount] => 0
    [max_amount] => 0
    [secure_post] =>
    [ipn_test] =>
    [no_shipping] => 0
    [address_override] => 0
    [cost_per_transaction] => 0
    [cost_percent_total] => 0
    [tax_id] => 0
)
0.00031495094299316

其他:

Array
(
    [paypal_merchant_email] => sample@ymail.com
    [paypal_verified_only] => 0
    [payment_currency] =>
    [sandbox] => 1
    [sandbox_merchant_email] => sample@ymail.com
    [payment_logos] =>
    [debug] => 0
    [status_pending] => W
    [status_success] => O
    [status_canceled] => D
    [countries] =>
    [min_amount] => 0
    [max_amount] => 0
    [secure_post] =>
    [ipn_test] =>
    [no_shipping] => 0
    [address_override] => 0
    [cost_per_transaction] => 0
    [cost_percent_total] => 0
    [tax_id] => 0
)
0.00026917457580566

Array
(
    [paypal_merchant_email] => sample@ymail.com
    [paypal_verified_only] => 0
    [payment_currency] =>
    [sandbox] => 1
    [sandbox_merchant_email] => sample@ymail.com
    [payment_logos] =>
    [debug] => 0
    [status_pending] => W
    [status_success] => O
    [status_canceled] => D
    [countries] =>
    [min_amount] => 0
    [max_amount] => 0
    [secure_post] =>
    [ipn_test] =>
    [no_shipping] => 0
    [address_override] => 0
    [cost_per_transaction] => 0
    [cost_percent_total] => 0
    [tax_id] => 0
)
0.00028419494628906
于 2013-09-24T04:03:04.013 回答
0

这可能是一个更优雅的解决方案,使用, ,str_getcsvarray_walkvenerable (提取键/值对)。使用您的数据:useparse_streach

$string = 'paypal_merchant_email="sample@ymail.com"|paypal_verified_only="0"|payment_currency=""|sandbox="1"|sandbox_merchant_email="sample@ymail.com"|payment_logos=""|debug="0"|status_pending="W"|status_success="O"|status_canceled="D"|countries=""|min_amount="0"|max_amount="0"|secure_post=""|ipn_test=""|no_shipping="0"|address_override="0"|cost_per_transaction="0"|cost_percent_total="0"|tax_id=0|';

想法是首先使用 str_getcsv 来解析 -separated|行,然后使用快速 PHP 函数遍历它。

$t = str_getcsv( $string, "|" );

此时,$t 是一个简单的数组,包含分解的“key=value”对。

$new = array();
array_walk( $t, function( &$a ) use (&$new) {
    parse_str( $a, $b);            # parses each line, returns key-value
    $c = each( $b );               # need to obtain key-value from above
    $new[$c["key"]] = $c["value"]; # simple assignment
});

这需要 PHP > 5.3.0,因为它在匿名函数中使用命名空间 (use)。但是,如果你做到了这一步, print_r( $new );给你这个:

Array (
    [paypal_merchant_email] => "sample@ymail.com"
    [paypal_verified_only] => "0"
    [payment_currency] => ""
    ...
于 2013-09-24T04:10:47.103 回答
0

有一个函数 parse_str() 但它用于 & 分隔符:http: //php.net/manual/en/function.parse-str.php

或者你可以用知识或 PHP 函数给别人留下深刻印象:

$str = 'a=13|b="string"|';

$rawarray = preg_split('/\|/', $str, -1, PREG_SPLIT_NO_EMPTY);
$keys = array_map(create_function('$a', '$r = preg_split("/=/", $a); return $r[0];'), $rawarray);
$values = array_map(create_function('$a', '$r = preg_split("/=/", $a, 2); return eval("return $r[1];");'), $rawarray);
$result = array_combine($keys, $values);

print_r($result);
于 2013-09-24T03:50:36.550 回答
0

如果您只需要一两个值,请使用正则表达式:

function get_value_from_string($key, $str) {
    return preg_match('/\|'.$key.'=([^\|]*)/', '|'.$str, $matches) ? eval("return $matches[1];") : null;
}
于 2013-09-24T04:05:43.133 回答
0

这是适合您的非常紧凑的解决方案:

preg_match_all('/\|(.*?)="(.*?)"/', '|'.$string, $matches);
$result = array_combine($matches[1], $matches[2]);

在这里测试它:https ://3v4l.org/rfBuc

于 2017-07-28T01:09:47.277 回答