如何将字符串解析为数组

时间:2013-09-24 03:32:32

标签: php

我在表格列中有以下值是否有任何选项可以获取sandbox值和merchant_email数组?我知道使用explode和循环是有效但任何其他智能选项  ? 像$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|

提前致谢!

6 个答案:

答案 0 :(得分: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
    [] => 
)

答案 1 :(得分: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

答案 2 :(得分:0)

有一个函数parse_str()但是它用于&amp;分隔符: 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);

答案 3 :(得分:0)

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

function get_value_from_string($key, $str) {
    return preg_match('/\|'.$key.'=([^\|]*)/', '|'.$str, $matches) ? eval("return $matches[1];") : null;
}

答案 4 :(得分:0)

使用str_getcsvarray_walkuseparse_str和尊敬的each(提取键/值对),这可能是更优雅的解决方案。使用您的数据:

$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来解析| - 分隔的行,然后使用快速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&gt; 5.3.0因为它在匿名函数中使用名称空间(use)。但是,如果你做到这一点, print_r( $new );为您提供了这个:

Array (
    [paypal_merchant_email] => "sample@ymail.com"
    [paypal_verified_only] => "0"
    [payment_currency] => ""
    ...

答案 5 :(得分:0)

以下是适合您的紧凑型解决方案:

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

在此测试:https://3v4l.org/rfBuc