Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/243.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何将字符串解析为数组_Php - Fatal编程技术网

Php 如何将字符串解析为数组

Php 如何将字符串解析为数组,php,Php,我在表列中有以下值是否有任何选项可以获取沙盒值和商户电子邮件类数组?我知道使用爆炸和循环是可行的,但任何其他聪明的选择 ? 类似于$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"|

我在表列中有以下值是否有任何选项可以获取
沙盒
值和
商户电子邮件
类数组?我知道使用爆炸和循环是可行的,但任何其他聪明的选择 ? 类似于
$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|

提前谢谢

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

或者,您可以用知识或PHP功能打动他人:

<?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
另一个:

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

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

$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\u getcsv
array\u walk
use
parse\u str
和古老的
每个
(提取键/值对)可能是一个更优雅的解决方案。使用您的数据:

$t = str_getcsv( $string, "|" );
想法是首先使用str_getcsv解析
分隔行,然后使用快速PHP函数遍历它

$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
});
此时,$t是一个简单的数组,包含分解的“key=value”对

这需要PHP>5.3.0,因为它在匿名函数中使用名称空间(use)。然而,如果你做到了这一点,
print\r($new)提供以下信息:

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

这是一个非常紧凑的解决方案:


在这里测试:

是否与您的意思类似?我认为OP知道如何使用explode等,但询问是否有更好的方法提供该字符串格式。我认为爆炸两次就足够了。。。另外,查看您当前使用的内容也会有所帮助。@Prix它不是来自paypal,Virtuemart正在以这种格式将支付网关详细信息保存在中DB@JobinJose我还向基准添加了一个使用
preg\u match\u all
的方法,这个方法可能是最简单的一个。谢谢你的回答。这与分解和循环相同,我正在寻找更好的选项bcoz,如果字符串长度较高,则循环需要时间。请参见我的编辑。只需将整个内容转换为关联数组就更容易了。感谢您的时间+1努力演示,这与普通explode()不同吗?您使用的是简单的正则表达式,使用explode()可以轻松完成。@AyeshK会建议您在此基础上做一些基准测试,因为它可能比使用explode更重,但是您只知道,通过实际操作,两者的差异将非常小。您的基准测试的意义是什么?没有意义,,你节省了几微秒。@RPM的意义是向他展示,他从一开始就可以做到这一点,看看哪种方法能最好地完成他需要做的事情,但请不要为此生气。@RPM哦,很抱歉,我实际上有3种不同的方法,上面有一个基准。@JobinJose似乎是
preg\u match\u all
在1k迭代中表现最佳。@Prix问题不是关于基准测试。这是关于解析字符串的。谢谢你的回答,没有朋友,我需要几乎所有的值。
$t = str_getcsv( $string, "|" );
$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
});
Array (
    [paypal_merchant_email] => "sample@ymail.com"
    [paypal_verified_only] => "0"
    [payment_currency] => ""
    ...
preg_match_all('/\|(.*?)="(.*?)"/', '|'.$string, $matches);
$result = array_combine($matches[1], $matches[2]);